android链接服务端数据库

来源:互联网 发布:癌症诊断书生成软件 编辑:程序博客网 时间:2024/06/06 18:44
  1. <?php
  2. header(\"Content-Type: text/html; charset=UTF-8\");

  3. $array=array(\'title\'=>\'name\',\'value\'=>\'dog\');
  4. echo json_encode($array);

  5. ?>

  6. android 端的代码 如下:
  7. package com.example.jsontest;

  8. import java.io.BufferedReader;
  9. import java.io.InputStreamReader;

  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.client.HttpClient;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.impl.client.DefaultHttpClient;
  14. import org.json.JSONObject;

  15. import android.os.Bundle;
  16. import android.app.Activity;
  17. import android.view.View;
  18. import android.widget.Button;

  19. import android.widget.EditText;

  20. public class MainActivity extends Activity {

  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.         Button btn = (Button) findViewById(R.id.getPhpJson);
  26.         btn.setOnClickListener(new View.OnClickListener() {

  27.             public void onClick(View v) {
  28.                 EditText edit = (EditText) findViewById(R.id.typeId);

  29.                 String url = \"http://192.168.191.1/test/json.php\";
  30.                 edit.setText(\"begin\");

  31.                 getServerJsonDataWithNoType(url, edit);
  32.             }
  33.         });
  34.     }

  35.     public void getServerJsonDataWithNoType(String url, EditText editText) {
  36.         int res = 0;
  37.         HttpClient client = new DefaultHttpClient();
  38.         StringBuilder str = new StringBuilder();
  39.         HttpGet httpGet = new HttpGet(url);
  40.         try {
  41.             HttpResponse httpRes = client.execute(httpGet);
  42.             httpRes = client.execute(httpGet);
  43.             res = httpRes.getStatusLine().getStatusCode();
  44.             if (res == 200) {
  45.                 BufferedReader buffer = new BufferedReader(
  46.                         new InputStreamReader(httpRes.getEntity().getContent()));
  47.                 for (String s = buffer.readLine(); s != null; s = buffer
  48.                         .readLine()) {

  49.                     str.append(s);
  50.                 }
  51.                 String newstr = new String(str.toString().getBytes(), \"UTF-8\");
  52.                 editText.setText(newstr);

  53.                 JSONObject json = new JSONObject(newstr);

  54.                 String title = json.getString(\"title\");

  55.                 String value = json.getString(\"value\");
  56.                 editText.setText(title + value);

  57.             } else {

  58.             }
  59.         } catch (Exception e) {

  60.             e.printStackTrace();

  61.         }

  62.     }

  63. }

  64. 注意要把字符串转为utf8编码,因为String 默认是unicode编码的。
  65. String newstr = new String(str.toString().getBytes(), \"UTF-8\");
  66. 再然后就是
  67. 别忘了在AndroidManifest.xml中给app加上权限:草坪销售
  68.  <uses-permission android:name=\"android.permission.INTERNET\" />
  69. 这样这个例子就OK了

0 0