2016-04-14 15 views
0

私のアンドロイドアプリケーションからJSONを使ってデータベースに値を追加しようとしています。アンドロイドスタジオを使用しているJSON

私は以下のコードをEclipseで以前使用していて、完璧に動作しました。今はAndroidスタジオを使って試していますが、動作しません。理由はわかりません!

コード:

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) 

私はエラーであるものを得ることはありませんか?

誰かが助けてくれますか?ありがとうございました!

答えて

1

価値がhtml形式で開始を受けたので、HTMLのエラーでリクエストをすることができ、JSONを受信して​​いないようです:

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

が、私は「それは私が入力した場合でも、JSON :( – LamaTat

+0

だ確信していますブラウザのリンクは、正しく書かれたJSONを返すでしょう! – LamaTat

+0

私は今、私は理由を知っていると思う、それはサーバーから来ているようです – LamaTat

関連する問題