从json和org.json中获取数据.抛出JSONException


Get data from json and org.json.JSONException is thrown

将通过http://localhost/getData.php接收以下json,但抛出异常

{"username":"not found","password":null}

日志我收到

02-19 17:31:54.745: E/JSON Parser(5277): Error parsing data org.json.JSONException: End of input at character 0 of 
02-19 17:31:59.185: E/JSON Parse(5277): ERROR

以下代码是异常抛出的方法

@Override
    protected String doInBackground(String... url){         
        try{
            String resultText = "";
            EditText edit = (EditText)findViewById(R.id.text_box);
            id = edit.getText().toString();
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("id",id));
            JSONObject json = jsonParser.HttpRequest("http://localhost/getData.php", params);
            resultText = json.getString("username");

        }catch(Exception e){
            e.printStackTrace();
            Log.e("JSON Parse","ERROR");
        }
        return "";
    }

公共类SimpleJsonParser{

public SimpleJsonParser(){
}

public JSONObject HttpRequest(String url, List<NameValuePair> params){
    InputStream input = null;
    JSONObject jObj = null;
    String json = "";
    String line;
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    paramsString = URLEncodedUtils.format(params,"utf-8");
    url += "?" + paramsString;
    HttpGet httpGet = new HttpGet(url);

    try{

        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            HttpEntity entity = response.getEntity();
            input = entity.getContent();
            BufferedReader reader = 
                    new BufferedReader(new InputStreamReader(input));

            while((line = reader.readLine())!=null){
                builder.append(line);
            }
            json = builder.toString();
        } else {
            Log.e("JSON Parser","Failed to download file");
        }
    }catch(ClientProtocolException e){
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
    try {
        jObj = new JSONObject(json);
        input.close();
    } catch (Exception e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    return jObj;
}

}

我的代码有什么问题吗?我提供的代码是发生异常抛出的地方

从您提到的内容来看,您似乎是通过模拟器来实现这一点的。

Emulator可能无法访问http://localhost//。您可以尝试使用、环回地址或ip地址进行访问。

HttpEntity返回的流中读取后,您永远不会验证是否实际收到了任何内容。这个异常告诉您在字符0(第一个字符)处没有输入——您没有任何要解析的内容;这是一个空字符串。您的StringBuilder从未附加任何内容。

public static void main(String[] args) throws JSONException
{
    String json = "{'"username'":'"not found'",'"password'":null}";
    JSONObject  jObj = new JSONObject(json);
}   

没有这样的例外。通常写一个小的测试用例会为你指明正确的方向。

正如其他人所提到的,这可能是因为你使用了错误的URL,或者你的参数?既然你要返回一个200 OK,那么你显然要连接到一个Web服务器并得到响应。。。我会检查一下PHP是否如您所期望的那样工作。

在simpleJarsonParser类中,我替换了以下代码

HttpClient httpClient = new DefaultHttpClient();

DefaultHttpClient httpClient = new DefaultHttpClient();

它是有效的。当然,我已经将localhost替换为10.0.2.2,因为我使用的是examplep和android模拟器。