我需要使用android + php,但php也回显null
????我不知道是否有任何错误?我搜索了一些信息,我检查了我添加了Thread,并且我已经包含了
<uses-permission android:name="android.permission.INTERNET" />
请给我一些建议!
PHP:
机器人:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.foodrestaurdetail);
Intent intent = this.getIntent();
name = intent.getStringExtra("name");
vendor_sn = intent.getStringExtra("vendor_sn");
Toast.makeText(getApplicationContext(),vendor_sn, Toast.LENGTH_SHORT).show();
getActionBar().setTitle(name);
// setupWebView();//載入Webview
txtMessage = name;
Thread t = new Thread(new sendPostRunnable(name));
t.start();
}
private String sendPostDataToInternet(String strTxt) {
// TODO Auto-generated method stub
HttpPost httpRequest = new HttpPost(MAP_URL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", strTxt));
try{
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200){
String strResult = EntityUtils.toString(httpResponse.getEntity());
return strResult;
}
}catch (ClientProtocolException e){
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}catch (IOException e){
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (Exception e){
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return null;
}
class sendPostRunnable implements Runnable{
String strTxt = null;
public sendPostRunnable(String strTxt) {
// TODO Auto-generated constructor stub
this.strTxt = strTxt;
}
@Override
public void run() {
// TODO Auto-generated method stub
String result = sendPostDataToInternet(strTxt);
mHandler.obtainMessage(REFRESH_DATA, result).sendToTarget();
}
}
答案 0 :(得分:0)
不推荐使用HttpPost。我建议使用Volley库简单,方便,实用。
这是一个例子:
// Tag used to cancel the request
String tag_string_req = "string_req";
String url = "http://api.androidhive.info/volley/string_response.html";
ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
StringRequest strReq = new StringRequest(Method.GET,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
//向请求队列添加请求 AppController.getInstance()。addToRequestQueue(strReq,tag_string_req);
使用参数的另一个例子:
// Tag used to cancel the request
String tag_json_obj = "json_obj_req";
String url = "http://api.androidhive.info/volley/person_object.json";
ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Androidhive");
params.put("email", "abc@androidhive.info");
params.put("password", "password123");
return params;
}
};
//向请求队列添加请求 AppController.getInstance()。addToRequestQueue(jsonObjReq,tag_json_obj);
这是一个很好的教程:
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/