2015-08-19 28 views
-2

試圖學習如何連接php android studio和基本示例json。 已經兩週了,我試圖連接到各種目錄,並總是得到錯誤。 這就是爲什麼我想了解一個基本的例子則繼續無法在android studio中轉換Json

我得到異常:

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

這是我main.java

public class MainActivity extends ActionBarActivity implements OnClickListener { 
     Button fetch; 
     TextView text; 
     EditText et; 


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

      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(MainActivity.this); 
      InputStream is = null ; 
      String result = ""; 
      protected void onPreExecute() { 
       progressDialog.setMessage("Fetching data..."); 
       progressDialog.show(); 
       progressDialog.setOnCancelListener(new OnCancelListener() { 
        @Override 
        public void onCancel(DialogInterface arg0) { 
         task.this.cancel(true); 
        } 
       }); 
      } 
      @Override 
      protected Void doInBackground(String... params) { 
       String url_select = "my site.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)); 
        StringBuilder sb = new StringBuilder(); 
        String line = ""; 
        while((line=br.readLine())!=null) 
        { 
         sb.append(line+"\n"); 
        } 
        is.close(); 
        result=sb.toString(); 
        Log.e("result",result); 

       } 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); 
        Log.e("rannn",Integer.toString(Jarray.length())); 
        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("name"); 
         String db_detail=""; 

         if(et.getText().toString().equalsIgnoreCase(name)) { 
          db_detail = Jasonobject.getString("quantity"); 
          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; 
      } 

     } 

    } 

這是來自於我的結果返回JSON PHP時,我在瀏覽器中運行它。

{"name":"10","quantity":"50"} 

,這是導致日誌之前我嘗試解析器。

<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("4149455d59914a7942887c4bd71ed6dc");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";location.href="mysite.php?ckattempt=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html> 
+0

分享您想分析的JSON和POJO! –

+1

'<...'不是json字符串。它是* html *。你想要連接哪個網址? –

回答

1

您發佈的請求沒有返回JSON。

這是您的日誌清楚地明顯:

錯誤的數據進行解析org.json.JSONException:價值<html><body>

調試您的服務器和應用程序,找出你爲什麼沒有得到JSON