php - 无法将 JSONObject 从 android 发布到 PHP

标签 php android

我正在尝试使用以下代码将 JSONObject 从 android 应用程序发布到 php web 应用程序。

String url = "http://10.0.2.2/test/"
URL rest_url = new URL(url);
JSONObject params = new JSONObject();
params.put("username","admin"); 

// Send POST data request
BufferedReader reader=null;

DataOutputStream printout;
URLConnection conn = rest_url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( params.toString());
wr.close();

当我尝试在 php 中检索用户名时,我得到的是空值。

 <?php echo $_POST['username'];
 ?>

最佳答案

为了避免在每个连接类中编写 HTTP 代码,我这样做了。

创建了单独的 http 类并放置了这段代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;


public class HttpConnect
{
    HttpClient httpClient;
    String Url;
    private static final String                 DEBUG_TAG = "TAG";


    public HttpConnect(String url) {
        this.Url = url;
        httpClient = new DefaultHttpClient();
    }

    public HttpEntity Get() throws ClientProtocolException, IOException{

        HttpGet httpGet = new HttpGet(Url);
        HttpResponse httpResponseGet = httpClient.execute(httpGet);
        HttpEntity resEntityGet = httpResponseGet.getEntity();
        return resEntityGet;
    }

    public HttpEntity Post(HashMap<String, String> c) throws ClientProtocolException, IOException{

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(Url);

        // set up post data
        ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
        Iterator<String> it = c.keySet().iterator();
        while (it.hasNext())
        {
            String key = it.next();
            nameValuePair.add(new BasicNameValuePair(key, c.get(key)));
        }

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8"));
        HttpResponse httpResponseGet = httpClient.execute(httpPost);
        HttpEntity resEntityGet = httpResponseGet.getEntity();
        return resEntityGet;

    }

}

并通过在 HashMap<String, String> obj = new HashMap<String, String>(); 中发布您的键值数据从您的类(class)访问此类

obj.put("username","admin");
HttpEntity resEntityGet = new HttpConnect("URL").Post(obj);

转换resEntityGet像这样串起来的实体

String responseString = EntityUtils.toString(resEntityGet); //response returned from your script

关于php - 无法将 JSONObject 从 android 发布到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32546260/

相关文章:

带链接的 PHP for() 循环

javascript - 如何将数据从一页上的表单发送到不同页面上的不同表单?

Android:如何使用 google-drive-sdk 访问电子表格?

android - 面向 fragment 的架构和后台堆栈

java - 将参数传递给异步类,使 param[0] 和 params[1] 包含不同的值

php - 多项选择类型,需要数组

php - 如何让分页在 WordPress 中为 get_posts() 工作?

php - 使用ajax加载laravel View

Android,FragmentTabHost - 如何去除蓝色?

android - google map android api v2 是否支持街景覆盖?