I am pretty new to Java programming. I am doing a Http Request with Async using the implementation Loopj (com.loopj.android:android-async-http:1.4.9).
The OnSuccess and OnFailure of the AsyncHttpResponseHandler are working fine.
New I want to return the HttpRespone the Main thread and display it on a new Activity (intent).
I found the method „public void onPostProcessResponse“ but the description tells the following: „Please note: post-processing does NOT run on the main thread, and thus any UI activities that you must perform should be properly dispatched to the app's UI thread.“ ( https://loopj.com/android-async-http/doc/com/loopj/android/http/AsyncHttpResponseHandler.html )
I really appreciate any help!!
Here is the code:
enter code here
package com.example.myapplication;
enter code here
import androidx.appcompat.app.AppCompatActivity;
enter code here
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.ResponseHandlerInterface;
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.HttpResponse;
public class HttpRequest extends AppCompatActivity {
private TextView eMail;
private Button CallWebService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_request);
eMail = (TextView) findViewById(R.id.textView);
CallWebService = (Button) findViewById(R.id.call_webservice);
Intent get_Email = getIntent(); //
String eMail_address = getIntent().getStringExtra("id_Email");
eMail.setText(eMail_address);
CallWebService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://jsonplaceholder.typicode.com/posts/1"; //genutzte Bib: implementation 'com.loopj.android:android-async-http:1.4.9'
new AsyncHttpClient().get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String urlResponse = new String(responseBody);
CallWebService.setText(urlResponse);
//String currentString = urlResponse;
//String [] separated = currentString.split(",");
//CallWebService.setText(separated[1]);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
CallWebService.setText("Error"); //response Body: Enthält die Antwort der URL Abfrage in Byte -> Umwandlung in string nötig
}
});
}
});
}
}
All the best, Simon
Use this in onSuccess();
runOnUiThread(new Runnable(){
String urlResponse = new String(responseBody);
CallWebService.setText(urlResponse);
}
);
invalidate the View must in UI Thread.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.