2012-09-10 52 views
2

我想從url獲取數據。在這種情況下,我已經得到了數據轉換成JSON已經在本地主機上運行完整的PHP(HTTP://localhost/adchara1/index.php/年= 1)從android中獲取數據url

這是PHP腳本

<?php 
mysql_connect("localhost","root",""); 
mysql_select_db("test"); 
$q=mysql_query("SELECT * FROM people 
WHERE 
birthyear>'".$_REQUEST['year']."'"); 
while($e=mysql_fetch_assoc($q)) 
     $output[]=$e; 
    print(json_encode($output)); 
    mysql_close(); ?> 

,這是導致

[{"id":"1","name":"kongkea","sex":"1","birthyear":"1990"}, {"id":"2","name":"thida","sex":"0","birthyear":"2000"}]?> 

我想用按一下按鈕,並顯示該結果的TextView

+0

實際上你想問什麼?android代碼相同還是你有什麼疑問? –

+0

在android中,我想從android中的textView中顯示結果的URL。 ex,在android中我有1button和1 TextView,我想要得到結果在URL'[{「id」:「1」,「name」:「kongkea」,「sex」:「1」,「birthyear」: 「1990」},{「id」:「2」,「name」:「thida」,「sex」:「0」,「birthyear」:「2000」}]?>'在TextView中顯示。 – kongkea

回答

8
public class MainActivity extends Activity { 

AsyncTask<Void, Void, Void> mTask; 
String jsonString; 

String url = "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=50cent&count=2"; 


Button b; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    b = (Button) findViewById(R.id.btnFetch); 
    final TextView tv = (TextView) findViewById(R.id.txtView); 

    mTask = new AsyncTask<Void, Void, Void>() { 

     @Override 
     protected Void doInBackground(Void... params) { 
      try { 
       jsonString = getJsonFromServer(url); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 

      tv.setText(jsonString); 

     } 

    }; 

    b.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      mTask.execute(); 
     } 
    }); 
} 


public static String getJsonFromServer(String url) throws IOException { 

    BufferedReader inputStream = null; 

    URL jsonUrl = new URL(url); 
    URLConnection dc = jsonUrl.openConnection(); 

    dc.setConnectTimeout(5000); 
    dc.setReadTimeout(5000); 

    inputStream = new BufferedReader(new InputStreamReader(
      dc.getInputStream())); 

    // read the JSON results into a string 
    String jsonResult = inputStream.readLine(); 
    return jsonResult; 
} 

} 

從服務器獲取jsonString用此方法後,你可以pa rse並在Json中顯示數據。

編輯:你會得到錯誤,因爲你試圖從服務器中獲取json異步task.You需要在後臺執行它。您可以使用線程或使用AsyncTask。

+0

感謝您的回答,但它不起作用。這裏是你的代碼。 [code](http://www.mediafire.com/download.php?15vdw54ct84zeqv) – kongkea

+0

請參閱我編輯的答案。我改變了一下,現在它工作... – osayilgan

+0

謝謝。有用。我使用按鈕來清除textview(tv.setText(「」))。並再次獲取數據,但錯誤。 ÿ? – kongkea

5

寫這樣的代碼在您的按鈕OnClickListener

 try { 
     String url = "http://YourIPAddress/adchara1/index.php/?year=1"; 
     HttpPost httppost = new HttpPost(url); 
     try { 
      HttpParams p = new BasicHttpParams(); 
      HttpClient httpclient = new DefaultHttpClient(p); 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      String responseBody = httpclient.execute(httppost, 
        responseHandler); 
      JSONArray jArray = new JSONArray(responseBody); 
      String text=""; 
      for (int i = 0; i < jArray.length(); i++) { 
       JSONObject e = jArray.getJSONObject(i); 
        text = text + "ID : "+e.getString("id")+"\n"; 
        text = text + "Name : "+e.getString("name")+"\n"; 
        text = text + "Sex : "+e.getString("sex")+"\n"; 
        text = text + "Birthyear : "+e.getString("birthyear")+"\n"; 
      } 
      Textview.setText(text); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } catch (Throwable t) { 
     Toast.makeText(this, "Request failed: " + t.toString(), 
       Toast.LENGTH_LONG).show(); 
     t.printStackTrace(); 
    } 

如果您有任何澄清通知我

+0

感謝您的回答,但它不起作用。 [項目](http://www.mediafire.com/download.php?5g12g4hs0ngnasl)。謝謝 – kongkea

1
Button button =(Button) findViewById(R.id.button); 
     TextView tv =(TextView) findViewById(R.id.textview); 
     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       String result = connectionFromServer("http://localhost/adchara1/index.php/?year=1"); 
       tv.setText(result); 
      } 
     }); 

public static String connectionFromServer(String url) throws IOException { 

      BufferedReader inputStream = null; 

      URL myurl = new URL(url); 
      URLConnection dc = myurl.openConnection(); 

      dc.setConnectTimeout(5000); 
      dc.setReadTimeout(5000); 

      inputStream = new BufferedReader(new InputStreamReader(
        dc.getInputStream())); 

      // read the JSON results into a string 
      String result = inputStream.readLine(); 
      return result; 
     } 

這將在textview中顯示結果。

+0

感謝您的回答,但它不起作用。這裏是你的代碼[http://www.mediafire.com/?as4kuid2iots4bx] – kongkea