Java 在android中执行HttpResponse时出现异常
我被HttpRequest方法的一个异常卡住了几个小时,我真的厌倦了这个。我做了谷歌调查,但我找不到任何解决办法。我需要做一个http请求,通过PHP脚本从数据库中获取数据。当我使用params为表中的所有行调用方法时,一切都进行得很顺利: url-“”方法- “获取”列表-空列表 但当我尝试获取带有参数的指定id行时,它会抛出一个异常: url-“”方法-“获取” 列表-[noteid=1] 我从类中的doInBackground方法调用它,该方法扩展了AsyncTask。 如果你能帮我的话,这个方法有问题。。谢谢Java 在android中执行HttpResponse时出现异常,java,android,json,exception,http-request,Java,Android,Json,Exception,Http Request,我被HttpRequest方法的一个异常卡住了几个小时,我真的厌倦了这个。我做了谷歌调查,但我找不到任何解决办法。我需要做一个http请求,通过PHP脚本从数据库中获取数据。当我使用params为表中的所有行调用方法时,一切都进行得很顺利: url-“”方法- “获取”列表-空列表 但当我尝试获取带有参数的指定id行时,它会抛出一个异常: url-“”方法-“获取” 列表-[noteid=1] 我从类中的doInBackground方法调用它,该方法扩展了AsyncTask。 如果你能帮我的话,
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
Log.e("URL", url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
听起来您要么没有编写,要么没有正确使用
AsyncTask
。这是我的版本,应该做你正在尝试的事情。接下来我会告诉你怎么称呼这个。有一些常见的错误,例如直接调用doInBackground
方法而不是execute
,因此这应该可以解决所有问题
要将此代码包装到异步任务中
,请创建以下类
:
public class MyJSONRequest extends AsyncTask<Void, Void, Void> {
String url;
String method;
List<NameValuePair> params;
public MyJSONRequest(String url, String method, List<NameValuePair> params) {
this.url = url;
this.method = method;
this.params = params;
}
@Override
public Void doInBackground(Void...) {
makeHttpRequest(url, method, params)
}
}
公共类MyJSONRequest扩展异步任务{
字符串url;
字符串方法;
列出参数;
公共MyJSONRequest(字符串url、字符串方法、列表参数){
this.url=url;
这个方法=方法;
this.params=params;
}
@凌驾
公共作废背景(作废…){
makeHttpRequest(url、方法、参数)
}
}
现在,要顺利完成任务,请执行以下操作:
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("noteid", "1"));
new MyJSONRequest("http://android-connection.cba.pl/get_note.php", "GET", list).execute();
List List=new ArrayList();
添加(新的BasicNameValuePair(“noteid”,“1”));
新的MyJSONRequest(“http://android-connection.cba.pl/get_note.php,“GET”,list.execute();
另外,您应该注意,
if(method==“POST”)
和if(method==“POST”)
将无法解决您的期望。您需要将它们更改为if(method.equals(“POST”)
和if(method.equals(“GET”))
您可以发布您收到的实际错误吗?Java字符串的相等性不能使用==,而是使用method.equals(“GET”)和method.equals(“POST”)进行检查。看起来这是在主线程上完成的,而不像您所写的那样,在AsyncTask中的后台线程上。在EditNoteActivity$GetNoteDetails
中有一个Runnable
正在调用JSONParser.makeHttpRequest2()
嗯,如果你真的读过OP的问题,他说他已经在后台线程中使用AsyncTask
来做这件事了。当然,这不是真的,但是告诉他使用AsyncTask
可能没有任何帮助。@DavidWasser一定错过了。为了反映这些细节,我改变了我的答案。事实上我是这样做的,我认为这是正确的
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("noteid", "1"));
new MyJSONRequest("http://android-connection.cba.pl/get_note.php", "GET", list).execute();