在PHP端,我收到了一个我们要发回给Android的JSON对象。 PHP代码很简单,就像我在几乎所有教程中看到的一样,回应JSON对象。
此行每隔一个查询创建一个例外。我做错了什么?
JSONObject json = jsonParser.makeHttpRequest(signInURL,
"POST", params1);
04-15 21:40:44.787: E/AndroidRuntime(6325): java.lang.RuntimeException: An error occured while executing doInBackground()
我正在遵循这些说明:
如何在我们的应用程序中使用json。
在我们的Android应用程序中,
编辑:问题实际上只是需要修复的PHP警告。谢谢大家!
答案 0 :(得分:3)
在AndroidManifest.xml中设置互联网权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
从android到服务器的Http请求
public String httpRequest(String url, String query, int methodtype){
try {
String reqUrl = url + query;
switch (methodtype) {
case 1:
HttpGet httpGet = new HttpGet(reqUrl);
httpResponse = httpClient.execute(httpGet);
break;
case 2:
HttpPost httpPost = new HttpPost(reqUrl);
httpResponse = httpClient.execute(httpPost);
break;
}
HttpEntity httpEntity = httpResponse.getEntity();
instrObj = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
//HandleException
} catch (ClientProtocolException e) {
//HandleException
} catch (IOException e) {
//HandleException
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(instrObj, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
instrObj.close();
json = sb.toString();
} catch (Exception e) {
}
return json;
}
为了将表单数据发布到服务器,使用HttpPost并从服务器中检索数据HttpGet被使用....
答案 1 :(得分:1)
有时候没有设置互联网权限。
要设置互联网权限,请执行以下操作:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
希望这会有所帮助.. :)
答案 2 :(得分:1)
试试这段代码
try {
URL Url = new URL(" --------");
HttpURLConnection connection = (HttpURLConnection) Url.openConnection();
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream);
int contentLength = connection.getContentLength();
char[] charArray = new char[contentLength];
reader.read(charArray);
JSONObject jsonResponse = new JSONObject(charArray.toString());
}
else {
Log.i("", "HTTP Response Code: " + responseCode);
}
}
catch (Exception e) {
}