[英]Cannot get Data into PHP webservice from JSON in Android
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Button btn = (Button) findViewById(R.id.btnInsert);
EditText editText = (EditText) findViewById(R.id.editText);
final String value = editText.getText().toString();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
JSONObject jsonobj = new JSONObject();
jsonobj.put("name", value);
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppostreq = new HttpPost("http://www.myurl.com/WebserviceInsert2.php");
StringEntity se = new StringEntity(jsonobj.toString());
Log.d("jsonobj is :>", String.valueOf(jsonobj));
se.setContentType("application/json;charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
httppostreq.setEntity(se);
HttpResponse httpresponse = httpclient.execute(httppostreq);
String responseText = null;
try {
responseText = EntityUtils.toString(httpresponse.getEntity()); // {"code":1}
} catch (Exception e) {
e.printStackTrace();
Log.i("Parse Exception", e + "");
}
Log.d("Response Text is : ", responseText);
JSONObject json = new JSONObject(responseText);
int code = json.getInt("code");
if (code == 1)
Toast.makeText(getApplicationContext(), "Successful", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.d("Error :>", String.valueOf(e));
}
}
});
}
}
PHP Code:
<?php
include("include/config.php");
$name=$_POST["name"];
//echo $name;
$flag['code']=0;
if($result=mysql_query("INSERT INTO test (name) VALUES ('".$name."')"))
{
$flag['code']=1;
}
print(json_encode($flag));
$obj->close();
?>
數據庫表字段“名稱”為空。 插入查詢成功觸發,但是..數據未插入表中。
我認為PHP文件沒有從android applicaiton(JSON)發送的POST方法中正確獲取“ name”參數。
您尚未發布實際的logcat,但可以嘗試以下代碼
HttpClient httpClient = new DefaultHttpClient();
//Get COding
HttpPost httpPost = new HttpPost(your_url);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("name", value));
//you can add more parameters here
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
response=EntityUtils.toString(httpEntity);
您可以添加或設置內容類型或“常規標題”,以了解更多信息http://developer.android.com/reference/org/apache/http/client/HttpClient.html
因為您使用GET,Method來請求該網址。 如果您向服務器發送任何請求而沒有提及,則HttpClient將假定該請求為GET,但是在您的PHP中,您提及了它的$ _POST [“ name”]; 如果替換= $ _ POST [“ name”]; 與_REQUEST [“ name”];
否則,您必須將請求類型設置為Post。
然后數據可能會插入到數據庫中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.