Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java json解析器不适用于froyo和gingerbread,但适用于ics_Java_Android_Mobile - Fatal编程技术网

Java json解析器不适用于froyo和gingerbread,但适用于ics

Java json解析器不适用于froyo和gingerbread,但适用于ics,java,android,mobile,Java,Android,Mobile,我有一个JSON解析器类,如果有可选参数传递给它,它将发送一个Httppost public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url, Object...

我有一个JSON解析器类,如果有可选参数传递给它,它将发送一个Httppost

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;

}

有人能帮我吗?froyo和gingerbread是否仅支持httpGet???

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

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

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

有趣的是,当我试图在ubuntu的Froyo和gingerbread模拟器中测试它时,它工作了。然而,当我试图在windows中这样做时,它根本不起作用。所以这已经正式结束:)

试着把它放到异步任务中

范例

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();
        }
    }
}
私有类权限EnableAsyncTask扩展了AsyncTask
{
private ProgressDialog mProgressDialog=null;
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
mProgressDialog=新建进度对话框(PermissionList.this);
mProgressDialog.setMessage(常量.MSG\u PROGRESSBAR\u WAITING);
mProgressDialog.setCancelable(假);
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.show();
}
@凌驾
受保护对象[]doInBackground(字符串…参数)
{
尝试
{
String Url=“您的Url”;
//你的逻辑
回归你的逻辑;
}
捕获(例外e)
{
e、 printStackTrace();
返回null;
}
}
@凌驾
受保护的void onPostExecute(对象[]对象)
{
onPostExecute(对象);
mProgressDialog.disclose();
尝试
{
你的逻辑
}
捕获(例外e)
{
e、 printStackTrace();
}
}
}

10-21 13:49:10.348:E/JSON解析器(471):解析数据org.JSON.JSONException时出错:输入结束于的字符0处。这就是错误信息。我可以在ICS和jellybean中使用这个解析器,但不能在froyo和gingerbread设备中使用。哦,看来你可能需要用另一种方法来实现这一点
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();
        }
    }
}