从JSON获取的数据和org.json.JSONException被抛出 [英] Get data from json and org.json.JSONException is thrown

查看:149
本文介绍了从JSON获取的数据和org.json.JSONException被抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下JSON将被接收,但通过 HTTP://localhost/getData.php ,但会抛出异常。

Following json will be received but through the http://localhost/getData.php but exception is thrown

{"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

继code是方法,其中异常抛出

Following code is the method where exception throw

@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 class 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;
}

}

任何问题我的code?我公司提供的code是例外抛出发生

Anything wrong with my code? The code I provided is where exception throws occur

推荐答案

在simpleJarsonParser课,我替换以下code

Inside simpleJarsonParser class, I replace the following code

HttpClient httpClient = new DefaultHttpClient();

DefaultHttpClient httpClient = new DefaultHttpClient();

和它的作品。当然,我也localhost替换为10.0.2.2为我使用的XAMPP和Android模拟器。

and it works. And of course I have replace localhost to 10.0.2.2 as I'm using xampp and android emulator.

这篇关于从JSON获取的数据和org.json.JSONException被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆