使用JSON后,为什么我在android studio中的代码有错误?

时间:2018-08-20 10:13:21

标签: php json

这是我第一次从php到Android Studio提取数据。我有点困惑,为什么我的catch异常有一个错误。

public class Conn_checkEmail extends AsyncTask<String,Void,String> {
    Context context;
    AlertDialog alertDialog;

    public Conn_checkEmail(Context context)
    {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Register Status");
    }

    @Override
    protected void onPostExecute(String s) {
        alertDialog.setMessage(s);
        alertDialog.show();

    }

    @Override
    protected String doInBackground(String... voids) {
        String result = "";
        String email = voids[0];
        String source = voids[1];
        //String[] ct_name = null;
        String get_source, get_email, get_password, get_firstname, get_lastname, get_username, get_gender, get_birthday, get_contact, get_profile_picture = "";



        String conn = "http://192.168.254.103/ITSP/checkRegister.php";

        try {

                URL url = new URL(conn);
                HttpURLConnection http = (HttpURLConnection) url.openConnection();
                http.setRequestMethod("POST");
                http.setDoInput(true);
                http.setDoOutput(true);
                OutputStream ops = http.getOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops, "UTF-8"));
                String data = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")
                        +"&&"+URLEncoder.encode("source","UTF-8")+"="+URLEncoder.encode(source,"UTF-8");

                writer.write(data);
                writer.flush();
                writer.close();
                ops.close();

                // paring data
                JSONArray jArray;
                try {
                    jArray = new JSONArray(result);
                    JSONObject json_data = null;
                    //ct_name = new String[jArray.length()];

                    for (int i = 0; i < jArray.length(); i++) {
                        json_data = jArray.getJSONObject(i);

                        //get value
                        //ct_name[i] = json_data.getString("CITY_NAME");
                        get_source = json_data.getString("source");

                        if(get_source.equals("old")){

                            Intent setup = new Intent(context, SetUpActivity.class);
                            get_email = json_data.getString("email");
                            get_password = json_data.getString("password");
                            get_username = json_data.getString("username");
                            get_firstname = json_data.getString("firstname");
                            get_lastname = json_data.getString("lastname");
                            get_gender = json_data.getString("gender");
                            get_birthday = json_data.getString("birthday");
                            get_contact = json_data.getString("contact");
                            get_profile_picture = json_data.getString("profile_picture");


                            //Create the bundle
                            Bundle bundle = new Bundle();

                            //Add your data to bundle
                            bundle.putString("source", "old_account");
                            bundle.putString("o_email", get_email);
                            bundle.putString("o_password", get_password);
                            bundle.putString("o_username", get_username);
                            bundle.putString("o_firstname", get_firstname);
                            bundle.putString("o_lastname", get_lastname);
                            bundle.putString("o_gender", get_gender);
                            bundle.putString("o_birthday", get_birthday);
                            bundle.putString("o_contact", get_contact);
                            bundle.putString("o_profile_picture", get_profile_picture);

                            //Add the bundle to the intent
                            setup.putExtras(bundle);
                            context.startActivity(setup);
                        }

                    }
                } catch (JSONException e1) {
                    Toast.makeText(context, "No City Found", Toast.LENGTH_LONG).show();
                }

                http.disconnect();
                return result;


        } catch (MalformedURLException e) {
            result = e.getMessage();
        } catch (IOException e) {
            result = e.getMessage();
        }


        return result;
    }
}

这是错误:

08-20 18:00:00.461 22034-22094/firebase.example.com.e_seefyapplication E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: firebase.example.com.e_seefyapplication, PID: 22034
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:309)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:200)
        at android.os.Handler.<init>(Handler.java:114)
        at android.widget.Toast$TN.<init>(Toast.java:345)
        at android.widget.Toast.<init>(Toast.java:101)
        at android.widget.Toast.makeText(Toast.java:259)
        at firebase.example.com.e_seefyapplications.Conn_checkEmail.doInBackground(Conn_checkEmail.java:128)
        at firebase.example.com.e_seefyapplications.Conn_checkEmail.doInBackground(Conn_checkEmail.java:26)
        at android.os.AsyncTask$2.call(AsyncTask.java:295)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
        at java.lang.Thread.run(Thread.java:818) 

错误行位于catch部分,这是-> catch(JSONException e1){Toast.makeText(context,“ No City Found”,Toast.LENGTH_LONG).show(); }。

我使用json_data.string()从JSON使用php获取值。但这似乎不正确吗?

0 个答案:

没有答案