问题
I would like download a html page in order to display some information in a TextView. Everytime, my program displays "error" in the TextView. Why doesn't my program work ?
I add in the AndroidManifest.xml file, this line:
<uses-permission android:name="android.permission.INTERNET" />
Here my activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context="blabla.test.mainactivity">
<textview
android:id="@+id/aff"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="error" />
</relativelayout>
And here my MainActivity.java:
package blabla.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
public static String test = "error";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://square.github.io/okhttp/";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("APp", e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
MainActivity.test = response.body().string();
}else{
Log.e("APp", "Error");
}
}
});
TextView aff = (TextView) findViewById(R.id.aff);
aff.setText(MainActivity.test);
}
}
I thank you in advance for your help :)
回答1:
I understood my error, I have to put my code into runOnUThread(). Thank you for your quick response ;)
Here my correction:
package blabla.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://square.github.io/okhttp/";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("APp", e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
final String aFinalString = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView aff = (TextView) findViewById(R.id.aff);
aff.setText(aFinalString);
}
});
} else {
Log.e("APp", "Error");
}
}
});
}
}
回答2:
The problem is simple enough. When you query is complete you do not update the TextView so as to display the needed information. Therefore it keeps its original message which is "error", defined in the activity_main.xml layout file.
来源:https://stackoverflow.com/questions/35541733/okhttp-android-download-a-html-page-and-display-this-content-in-a-view