2014-04-14 177 views
0

我無法弄清楚這段代碼有什麼問題,它應該從我創建的php頁面獲取數據。 我使用Android SDK的Eclipse。Android連接使用PHP Mysql

package com.myproject.myproject2; 


import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.ParseException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 
public class EntList extends Activity { 

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

     JSONArray jsonArray = null; 

     String jsonString = null; 

     StringBuilder stringBuilder = null; 

     InputStream inStream = null; 

     TextView tv = (TextView) findViewById(R.id.listtitle); 



     ArrayList<NameValuePair> nVPArray = new ArrayList<NameValuePair>(); //This is empty because you're asking for data 
     try{ 
      //Connect to your script, and save get an object to read the data (inStream) 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost post = new HttpPost("http://sawtaljabal.com/ar/android_connect/test.php"); // Be sure to replace with your actual script 
      post.setEntity(new UrlEncodedFormEntity(nVPArray)); 
      HttpResponse postResponse = client.execute(post); 
      HttpEntity responseEntity = postResponse.getEntity(); 
      inStream = responseEntity.getContent(); 
      }catch(Exception e){ 
       Log.e("Error connecting", e.getLocalizedMessage()); 
      } 

     try{ 
      //read the stream to a single JSON string 
       BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"iso-8859-1"), 10); // iso-8859-1 is the character converter 
       stringBuilder = new StringBuilder(); 
       stringBuilder.append(reader.readLine() + "\n"); 

       String line="0"; 
       while ((line = reader.readLine()) != null) { 
           stringBuilder.append(line + "\n"); 
       } 
       inStream.close(); 
       jsonString = stringBuilder.toString(); 
       }catch(Exception e){ 
         Log.e("Error creating JSON string", e.getLocalizedMessage()); 
       } 


     try{ 
       //Turn the JSON string into an array of JSON objects 
       jsonArray = new JSONArray(jsonString); 
       JSONObject jsonObject = null; 
       for(int i=0;i< jsonArray.length();i++){ 
        jsonObject = jsonArray.getJSONObject(i); 
        //do something with the object, like String data = jsonObject.getString("KEY"); 
        String data = jsonObject.getString("n_t"); 
        tv.setText(data); 
       } 
       } 
       catch(JSONException e){ 
       e.printStackTrace(); 
       } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

我什至不知道該怎麼嘗試,請你幫我,這是我的第一個應用程序。

+0

logcat中的響應是什麼? – KOTIOS

+0

可能你會得到'Networkonmainthreadexception'.Because你正在執行'OnCreate'內的網絡操作。您應該使用異步任務。 http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Hariharan

+0

你想我告訴你在LogCat中的所有錯誤? – xyzdev

回答

0

要使用Android 首先你要調用Web服務連接php(可以是php返回頁面json),致電該頁面應該在AsynTask類。 然後您需要以正確的方式解析json

你可以嘗試任何好的教程。嘗試this

+0

感謝兄弟,這幫助了我很多。 – xyzdev

+0

歡迎,正確的方式在http:// stackoverflow中表示感謝。com /,接受答案並且投票回答答案。 – Mobi

+0

我是新來的,信譽低,我不能投票抱歉 – xyzdev

0

你確定,你得到的字符串是JSON Array。我認爲它應該是JSON object,你可以通過它JSON Array

這意味着

// try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(jsonString);   
    } catch (JSONException e) { 
    } 

,然後分析從JSON對象JSON數組。

結賬this鏈接,這可能是有幫助的。

也可以在AsynTask或不同的線程中進行所有網絡調用。

0

試試我的代碼, 它`投注者使用ResponseHandler所就會直接轉換成JSON響應字符串,你現在會從輸入流免費,之後parase你的代碼,祝你好運... :)

For efficient use of this code:請創建一個ASY任務類,並把這個代碼doinbackbrd方法

  try { 
      ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>(2); 
      nvp.add(new BasicNameValuePair("key1", "value1")); 
      nvp.add(new BasicNameValuePair("key2", "value2")); 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = newHttpPost("http://sawtaljabal.com/ar/android_connect/test.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nvp)); 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      String responseBody = httpclient.execute(httppost, responseHandler); 
      // Log.i("response", responseBody); 
     } catch (Exception e) { 
      Log.i("error1", "" + e.toString()); 
     }