1

如果有可选参数传递给它,我有一个发送 Httppost 的 JSON Parser 类。

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url, Object... params) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        if(params.length > 0){
            httpPost.setEntity(new UrlEncodedFormEntity((List<? extends NameValuePair>)params[0]));
        }

        HttpResponse httpResponse = httpClient.execute(httpPost);
        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();
    } 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;

}

}

这适用于我的 ICS 和 JellyBean 设备,但不适用于 Froyo 和 Gingerbread。错误信息是:

10-21 13:49:10.348: E/Buffer Error(471): Error converting result java.lang.NullPointerException
10-21 13:49:10.348: E/JSON Parser(471): Error parsing data org.json.JSONException: End of input at character 0 of 

有人可以帮我吗?froyo 和姜饼是否只支持 httpGet?

4

3 回答 3

0

首先检查您调用此 json 字符串的文件是否存在会破坏 json 格式的无效字符。当我从 php 文件中获取数据时遇到同样的问题时,这发生在我身上,我的 php 文件发送了一些额外的错误,导致解析到 jSONObject 失败

   try {
    jObj = new JSONObject(json);
    } catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

所以这就是我在那里出错的原因

于 2012-10-21T14:10:13.447 回答
0

有趣的是,当我尝试在 ubuntu 的 Froyo 和姜饼模拟器中测试它时,它可以工作。但是,当我尝试在 Windows 中执行此操作时,它根本不起作用。所以这是正式关闭:)

于 2012-10-21T22:22:03.217 回答
0

尝试将其放入 asynctask

例子

private class PermissionEnableAsyncTask extends AsyncTask<String, Void, Object[]>
{
    private ProgressDialog mProgressDialog = null;
    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(PermissionList.this);
        mProgressDialog.setMessage(Constant.MSG_PROGRESSBAR_WAITING);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();
    }

    @Override
    protected Object[] doInBackground(String... params) 
    {
        try
        {
            String Url = "your URL";
                    //your logic
        return yourlogic;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;    
        }
    }

    @Override
    protected void onPostExecute(Object[] object)
    {
        super.onPostExecute(object);
        mProgressDialog.dismiss();
        try 
        {
                            your logic          
                    }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
    }
}
于 2012-10-22T05:28:12.377 回答