java.lang.String类型的值异常无法转换为JSONObject

时间:2018-04-20 16:50:52

标签: java android

PHP文件工作正常。我似乎没有在这段代码中发现问题。为什么我得到那个例外? outputStream中有错误吗?是否有另一种方法可用于传递/获取数据?

    @Override
    protected String doInBackground(String... params) {
        String email = params[0];
        String password = params[1];
        String data="";
        int tmp;

        try {
            URL url = new URL("http://10.0.3.2/magasin/login.php");
            String urlParams = "email="+email+"&password="+password;

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(true);
            OutputStream os = httpURLConnection.getOutputStream();
            os.write(urlParams.getBytes());
            os.flush();
            os.close();

            InputStream is = httpURLConnection.getInputStream();
            while((tmp=is.read())!=-1){
                data+= (char)tmp;
            }

            is.close();
            httpURLConnection.disconnect();

            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return "Exception: "+e.getMessage();
        } 
    }

    @Override
    protected void onPostExecute(String s) {
        String con=null,err=null;
        try {
            JSONObject root = new JSONObject(s);
            JSONObject user_data = root.getJSONObject("user_data");
            con = user_data.getString("con");

        } catch (JSONException e) {
            e.printStackTrace();
            err = "Exception: "+e.getMessage();
        }
        Toast.makeText(ctx, err+"", Toast.LENGTH_SHORT).show();

    }

0 个答案:

没有答案