Android + Perl + MySQL

Осы бейнежазбада Android қосымшамен MySQL- ДерекҚорды байланыстыру әдісі көрсетілген.
https://youtu.be/-ezMzwD5md4
Байланыс жасау үшін Perl, Cronet кітәпханасы қолданылды.
Cronet жәйлі осы жерде оқуға болады:
https://developer.android.com/guide/topics/connectivity/cronet/start#response
https://chromium.googlesource.com/chromium/src/+/lkgr/components/cronet/README.md
Бағдарлама мәтіні:
MyCallBack.java:

public class MyCallBack extends UrlRequest.Callback {
<code>ByteBuffer myBuffer;
String payload;

@Override
public void onRedirectReceived(UrlRequest request,
                               UrlResponseInfo responseInfo, String newLocationUrl) {</code>
/* if (followRedirect) {
// Let's tell Cronet to follow the redirect!
request.followRedirect();
} else {
// Not worth following the redirect? Abandon the request.
request.cancel();
}
*/
}
<code>@Override
public void onResponseStarted(UrlRequest request,
                              UrlResponseInfo responseInfo) {
    // Now we have response headers!
    int httpStatusCode = responseInfo.getHttpStatusCode();
    long countBytes = responseInfo.getReceivedByteCount();
    if (httpStatusCode == 200) {
        // Success! Let's tell Cronet to read the response body.
        Log.i("MyCallback", "Status 200. " + responseInfo);
        myBuffer = ByteBuffer.allocateDirect((int)countBytes);
        request.read(myBuffer);

    } else if (httpStatusCode == 503) {
        // Do something. Note that 4XX and 5XX are not considered
        // errors from Cronet's perspective since the response is
        // successfully read.
    }</code>
// mResponseHeaders = responseInfo.getAllHeaders();
}
<code>@Override
public void onReadCompleted(UrlRequest request,
                            UrlResponseInfo responseInfo, ByteBuffer byteBuffer) {
    // Response body is available.
    Log.i("MyCallback", "Request completed " + byteBuffer);
    read(byteBuffer);
    // Let's tell Cronet to continue reading the response body or
    // inform us that the response is complete!</code>
// request.read(myBuffer);
}
<code>@Override
public void onSucceeded(UrlRequest request,
                        UrlResponseInfo responseInfo) {
    // Request has completed successfully!
    Log.i("MyCallback", "Request succeded. ");
}

@Override
public void onFailed(UrlRequest request,
                     UrlResponseInfo responseInfo, CronetException error) {
    // Request has failed. responseInfo might be null.
    Log.e("MyCallback", "Request failed. " + error.getMessage());
    // Maybe handle error here. Typical errors include hostname
    // not resolved, connection to server refused, etc.
}

public void read(ByteBuffer bb){
    String converted = null;
    try {
        converted = new String (bb.array(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    int basy =  converted.indexOf ("<!--basy-->") + 11;
    int songy =  converted.indexOf ("<!--songy-->");
    payload = converted.substring(basy, songy);
    Log.i("MyCallback", "Attempt to read: " + payload);
}

public String getPayload(){
    return payload;
}</code>
}
MainActivity.java:
public class MainActivity extends Activity {
MyCallBack myCallBack = new MyCallBack();
<code>@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void Click(View view) {

            CronetEngine.Builder engineBuilder = new CronetEngine.Builder(this);
    CronetEngine engine = engineBuilder.build();
    Executor executor = Executors.newSingleThreadExecutor();
    UrlRequest.Builder requestBuilder = engine.newUrlRequestBuilder(
            "https://www.qazaqtili.org/kzsozderi/total.cgi", myCallBack, executor);
    UrlRequest request = requestBuilder.build();
    request.start();
}

public void Click2(View view){
    TextView tv = findViewById(R.id.text);
    String ma_payload = myCallBack.getPayload();
    tv.setText(ma_payload);
}</code>
}

  • 0
0 пікір
Тек тіркелген қолданушылар ғана пікір қалдыра алады.