Android + Perl + MySQL
-
Prankenzilla
- Android / 30 сәуір 2020, 23:00
- 657
Осы бейнежазбада 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 {
// Let's tell Cronet to follow the redirect!
request.followRedirect();
} else {
// Not worth following the redirect? Abandon the request.
request.cancel();
}
*/
}
}
}
MainActivity.java:
public class MainActivity extends Activity {
MyCallBack myCallBack = new MyCallBack();
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 {
ByteBuffer myBuffer;
String payload;
@Override
public void onRedirectReceived(UrlRequest request,
UrlResponseInfo responseInfo, String newLocationUrl) {/* if (followRedirect) {// Let's tell Cronet to follow the redirect!
request.followRedirect();
} else {
// Not worth following the redirect? Abandon the request.
request.cancel();
}
*/
}
@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.
}// mResponseHeaders = responseInfo.getAllHeaders();}
@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!// request.read(myBuffer);}
@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 ("") + 11;
int songy = converted.indexOf ("");
payload = converted.substring(basy, songy);
Log.i("MyCallback", "Attempt to read: " + payload);
}
public String getPayload(){
return payload;
}}MainActivity.java:
public class MainActivity extends Activity {
MyCallBack myCallBack = new MyCallBack();
@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);
}}
-
0