Java Android HTTP获取错误
对不起,我是java和android的新手。 我想理解为什么它总是响应错误。 谢谢大家!Java Android HTTP获取错误,java,android,http,get,Java,Android,Http,Get,对不起,我是java和android的新手。 我想理解为什么它总是响应错误。 谢谢大家! try { HttpClient client = new DefaultHttpClient(); String getURL = "http://www.google.com"; HttpGet get = new HttpGet(getURL); HttpResponse responseGet = c
try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://www.google.com";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
//e.printStackTrace();
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
进口
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
是的,我进入了互联网
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testhttp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
似乎您正试图在UI线程中发出
http
请求
将您的请求放在另一个线程中。您也可以使用此代码发出HTTP请求:
public class RequestClient extends AsyncTask<String, Void, String>{
Context context;
public RequestClient(Context c) {
context = c;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... aurl){
String responseString="";
HttpClient client = null;
try {
client = new DefaultHttpClient();
HttpGet get = new HttpGet(LoginActivity.url);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
responseString = EntityUtils.toString(resEntityGet);
Log.i("GET RESPONSE", responseString.trim());
}
} catch (Exception e) {
Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString());
}
Log.d("ANDRO_ASYNC_RESPONSE", responseString.trim());
client.getConnectionManager().shutdown();
return responseString.trim();
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
}
}
public类RequestClient扩展异步任务{
语境;
公共请求客户端(上下文c){
上下文=c;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的字符串背景(字符串…aurl){
字符串responseString=“”;
HttpClient=null;
试一试{
client=新的DefaultHttpClient();
HttpGet=newhttpget(LoginActivity.url);
HttpResponse responseGet=client.execute(get);
HttpEntity-resEntityGet=responseGet.getEntity();
如果(resEntityGet!=null){
responseString=EntityUtils.toString(当前设置);
Log.i(“获取响应”,responseString.trim());
}
}捕获(例外e){
Log.d(“ANDRO_ASYNC_ERROR”,“ERROR is”+e.toString());
}
Log.d(“ANDRO_异步_响应”,responseString.trim());
client.getConnectionManager().shutdown();
返回responseString.trim();
}
@凌驾
受保护的void onPostExecute(字符串响应){
super.onPostExecute(响应);
}
}
使用LogCat检查与“错误”相关的Java堆栈跟踪。如果您不理解堆栈跟踪,请编辑您的问题并将其发布到此处。取消注释e.printStackTrace()
,查看logcatsee@user2068102 remove Toast.makeText(getApplicationContext(),“ok”,Toast.LENGTH_LONG).show()中的异常情况;从try块和catch块,使用Log.d(“OK”、“OK”);在try and catch块上,在logCat中查看结果!如果要使用Toast,请将其用作:runOnUiThread(new Runnable(){@Override public void run(){Toast.makeText(…).show();});