2015-10-20 25 views
-1

我已經存儲了一些數據存儲在web上的mysql數據庫。我在應用程序中使用web腳本和JSON對象,但是在解析數據時出現錯誤。通過php web服務連接android與sql

我能夠看到來自MySql服務器的網頁中的數據。但是,當運行應用程序並嘗試獲取數據時,Process Dialogue不會結束,並從catch塊中拋出下面的分析錯誤。

錯誤日誌

Error parsing data org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONArray 

這裏是我的代碼。 AddTopic.java

public class AddTopic extends Activity implements View.OnClickListener { 
Button fetch; 
TextView text; 
EditText et; 

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

    fetch= (Button) findViewById(R.id.fetch); 
    text = (TextView) findViewById(R.id.text); 
    et = (EditText) findViewById(R.id.et); 
    fetch.setOnClickListener(this); 
} 

class task extends AsyncTask<String, String, Void> 
{ 
    private ProgressDialog progressDialog = new ProgressDialog(AddTopicFragment.this); 
    InputStream is = null ; 
    String result = ""; 
    protected void onPreExecute() { 
     progressDialog.setMessage("Fetching data..."); 
     progressDialog.show(); 
     progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 
      @Override 
      public void onCancel(DialogInterface arg0) { 
       task.this.cancel(true); 
      } 
     }); 
    } 
    @Override 
    protected Void doInBackground(String... params) { 
     String url_select = "http://swachapp.byethost32.com/demo.php"; 

     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url_select); 

     ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); 

     try { 
      httpPost.setEntity(new UrlEncodedFormEntity(param)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 

      //read content 
      is = httpEntity.getContent(); 


     } catch (Exception e) { 

      Log.e("log_tag", "Error in http connection "+e.toString()); 
      //Toast.makeText(MainActivity.this, "Please Try Again", Toast.LENGTH_LONG).show(); 
     } 
     try { 
      BufferedReader br = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = ""; 
      while((line=br.readLine())!=null) 
      { 
       sb.append(line+"\n"); 
      } 
      is.close(); 
      result=sb.toString(); 

     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("log_tag", "Error converting result "+e.toString()); 
     } 

     return null; 

    } 
    protected void onPostExecute(Void v) { 

     // ambil data dari Json database 
     try { 
      JSONArray Jarray = new JSONArray(result); 
      for(int i=0;i<Jarray.length();i++) 
      { 
       JSONObject Jasonobject = null; 
       //text_1 = (TextView)findViewById(R.id.txt1); 
       Jasonobject = Jarray.getJSONObject(i); 

       //get an output on the screen 
       //String id = Jasonobject.getString("id"); 
       String name = Jasonobject.getString("headings"); 
       String db_detail=""; 

       if(et.getText().toString().equalsIgnoreCase(name)) { 
        db_detail = Jasonobject.getString("detailstory"); 
        text.setText(db_detail); 
        break; 
       } 
       //text_1.append(id+"\t\t"+name+"\t\t"+password+"\t\t"+"\n"); 

      } 
      this.progressDialog.dismiss(); 

     } catch (Exception e) { 
      // TODO: handle exception 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 
    } 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()) { 
     case R.id.fetch : new task().execute();break; 
    } 

} 
} 

感謝您的時間。

回答

0

該問題與Android或PHP無關。它與您的託管服務器有關。它期望Cookie和JavaScript來處理請求。當我在Google Chrome中瀏覽網址時,會將結果顯示爲JSONArray。當我嘗試使用Android HttpClient運行網址時,它產生的結果爲 「....」

請在下面的鏈接中查看答案。 Getting HTML response instead of JSON in android