Android中获取PHP服务器端Json回到数据注意事项
项目中为了节省用户通过3G网络访问系统内容的流量,决定采用Json的方式将服务器数据传递过来。而不是直接使用HTML的方式传输。
测试环境为:
XAMPP中的PHP + MYSQL+ Windows 7+Eclipse+本地网络
Android中的效果图:
在IE中访问地址显示的JSon数据为:
{"title":"TTT","id":1,"value":"TTT"}
|
java代码:
<?php
header( "Content-Type:
text/html; charset=UTF-8" );
$type
= $_GET [ 'type' ];
if ( $type
== 1)
{
//$obj->title
= "Test";
//$obj->id
= 1;
//$obj->value
= urlencode("TTT");
//echo
urldecode ( json_encode ($obj));
$array
= array (
'title' => 'TTT' ,
'id' =>1,
'value' =>urlencode( "哈哈" ));
echo
urldecode(json_encode( $array ));
}
?>
package com.jouhu; import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class PHPJsonActivity extends Activity { /** Called when the activity is first created. */ private String Tag = "PHPJsonActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //EditText edit = (EditText)findViewById(R.id.editText1); //String url = "http://88.88.88.200:8888/phpjson/index.php?type=1"; //getServerJsonDataWithNoType(url,edit); Button btn = (Button)findViewById(R.id.button1); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EditText edit = (EditText)findViewById(R.id.editText1); String url = "http://88.88.88.200:8888/phpjson/index.php?type=1"; getServerJsonDataWithNoType(url,edit); } }); } public void getServerJsonDataWithNoType(String url,EditText editText) { int res = 0; HttpClient client = new DefaultHttpClient(); StringBuilder str = new StringBuilder(); HttpGet httpGet = new HttpGet(url); try { HttpResponse httpRes = client.execute(httpGet); httpRes = client.execute(httpGet); res = httpRes.getStatusLine().getStatusCode(); if(res == 200) { BufferedReader buffer = new BufferedReader(new InputStreamReader(httpRes.getEntity().getContent())); for(String s = buffer.readLine(); s != null ; s = buffer.readLine()) { str.append(s); } //String out = EntityUtils.toString(httpRes.getEntity().getContent(), "UTF-8"); //StringBuilder sb = new StringBuilder() Log.i(Tag,str.toString()); try { //JSONObject json = new JSONObject(str.toString()).getJSONObject("content"); JSONObject json = new JSONObject(str.toString()); String title = json.getString("title"); Log.i(Tag,title); int id = json.getInt("id"); String value = json.getString("value"); Log.i(Tag,value); editText.setText("Title:" + title + " ID:" + id + " Value:" + value); } catch(JSONException e) { Log.i(Tag, e.getLocalizedMessage()); //buffer.close(); e.printStackTrace(); } } else { Log.i(Tag, "HttpGet Error"); } } catch(Exception e) { Log.i(Tag, "Exception"); } } } 1 出现Value of type java.lang.String cannot be converted to JSONObject.这是由于我们的php或者其他服务器脚本有BOM头造成的。可以通过Editplus或者EmEditor或者UltraEditor进行删除。具体可以参考如何用去掉UTF-8的BOM头
2 记得在AndroidManifest.XML中加入
3 下面应该处理的问题是 中文以及根据数据库中的数据使用ListView或GroupView之类的生成UI。 主参考文章: http://www.cnblogs.com/tt_mc/archive/2011/01/04/1925327.html http://www.itxue.com/html/caozuoxitong/WINNT/20101123/7077.html 参考文章: |