我有一个从我的应用程序的网站获取JSON对象的类。 不幸的是,我遇到了一个问题,某些手机出现错误400'错误请求'结果代码,大多数手机都能正常使用此代码。
任何人都知道这对所有手机都不起作用吗?
public class MyAppJSON {
private String params;
public MyAppJSON(String params) {
this.params = params;
}
public JSONObject readJSON() {
JSONObject result = null;
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.myapp.com/json.php?action=" + this.params);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
else {
Log.e(MyAppJSON.class.toString(), "Failed to download file");
}
result = new JSONObject(builder.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
return result;
}
}