【问题标题】:JSON using android studio使用安卓工作室的 JSON
【发布时间】:2016-04-14 18:22:49
【问题描述】:

我正在尝试通过 JSON 从我的 android 应用程序向我的数据库添加一些值。

我之前在 eclipse 中使用过以下代码并且运行良好,现在我使用 android studio 尝试它,但它不工作我不知道为什么!

代码:

  public class Main2Activity extends AppCompatActivity {
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText ID;
EditText fname;
EditText lname;
EditText phone;
Button addbtn;

// url to create new product
private static String url_create_product = "http://www.lamia.byethost18.com/add_info.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

String H,Q,C,Ls;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);


    ID = (EditText) findViewById(R.id.ID);
    lname = (EditText) findViewById(R.id.lname);
    fname = (EditText) findViewById(R.id.fname);
    phone = (EditText) findViewById(R.id.phone);

     H = ID.getText().toString();
     Q = lname.getText().toString();
     C = fname.getText().toString();
     Ls = phone.getText().toString();

    addbtn = (Button) findViewById(R.id.addbtn);
    addbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            new CreateNewProduct().execute();

        }
    });


}

class CreateNewProduct extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Main2Activity.this);
        pDialog.setMessage("Creating Product..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {


        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ID", H));
        params.add(new BasicNameValuePair("lname", Q));
        params.add(new BasicNameValuePair("fname", C));
        params.add(new BasicNameValuePair("phone", Ls));



        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
               // Intent i = new Intent(getApplicationContext(), AdminExercise.class);
               // startActivity(i);

                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }

}


  }

我收到了这个错误:

   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/JSON Parser: Error parsing data org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: Process: com.example.hatim.maps, PID: 1934
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:309)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at com.example.hatim.maps.Main2Activity$CreateNewProduct.doInBackground(Main2Activity.java:113)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at com.example.hatim.maps.Main2Activity$CreateNewProduct.doInBackground(Main2Activity.java:77)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
   04-14 21:08:36.214 1934-1980/com.example.hatim.maps E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)

我不明白是什么错误?

有人可以帮忙吗?谢谢!

【问题讨论】:

    标签: java android json android-studio


    【解决方案1】:

    您的请求似乎没有收到 JSON,可能是 HTML 错误,因为收到的值以 html 格式开头:

    E/JSON Parser: Error parsing data org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject
    

    【讨论】:

    • 但我确定它是 JSON :(
    • 即使我在浏览器中输入链接,它也会返回正确写入的JSON!
    • 我想现在我知道原因了,它似乎来自服务器 byethost
    猜你喜欢
    • 2019-04-24
    • 2018-01-29
    • 2013-07-08
    • 2017-04-17
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多