无法将数据从 Android 中的 JSON 导入 PHP 网络服务


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();
?>

数据库表字段"name"为空。插入查询成功触发,但是..数据未插入到表中。

我认为PHP文件没有从android应用程序(JSON(发送的POST方法中获得正确的"名称"参数。

你还没有发布实际的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 请求该 url。如果您向服务器发送任何请求而没有提及,那么 HttpClient 将假定该请求为 GET,但在您的 PHP 中您提到了它的 $_POST["名称"];如果替换 =$_POST["名称"];带有_REQUEST["名称"];

否则,您必须将请求类型设置为"发布"。

然后数据可能会插入到数据库中。