2016-04-14 48 views
0

Veritabanıma JSON üzerinden android uygulamamdan bazı değerler eklemeye çalışıyorum.JSON android studio kullanarak

Aşağıdaki kodu eclipse ile daha önce kullandım ve mükemmel çalıştı, şimdi android stüdyosu kullanarak deniyorum ve çalışmıyor, nedenini bilmiyorum!

kodu:

bu hata var
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) 

hata nedir alamadım?

Birisi yardım edebilir mi? teşekkür ederim!

cevap

1
değer html formatında ile başlar karşılaştığımız için istek de, bir HTML hatası bir JSON edilir olabilir almıyor gibi görünüyor

:

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

ama "Ben girmek bile o :( – LamaTat

+0

JSON var eminim Tarayıcıda link, doğru yazılmış JSON dönecektir! – LamaTat

+0

Ben şimdi nedenini biliyorum, sunucu byethost gibi görünüyor – LamaTat