1

我有一个从我的应用程序网站获取 JSON 对象的类。不幸的是,我遇到了一个问题,某些手机收到错误 400 'Bad Request' 结果代码,而大多数手机都可以正常使用此代码。

谁知道这不适用于所有手机?

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;
    }
}
4

0 回答 0