java - Android JSON通讯

原文 标签 java php android json web-services

我想在Android设备上的PHP服务器上执行以下查询:

{
    "command": "REGISTER",
    "data": {
        "email": "EMAIL",
        "login": "LOGIN",
        "password": "PASSWORD",
        "language": "USER_LANGUAGE"      
    }




public class AysnchronousTaskPost extends AsyncTask<String, Long, String> {
    private static final String PATH = "http://alphabravo.com/myapp/api.php";

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        InputStream mInputStream;
        String lineString = "";
        String resultString = "";
        StringBuilder mStringBuilder = null;

        List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();


        nameValuePairList.add(new BasicNameValuePair("command","register"));
        nameValuePairList.add(new BasicNameValuePair("email","abc@gmail.com"));
        nameValuePairList.add(new BasicNameValuePair("login", "abc"));
        nameValuePairList.add(new BasicNameValuePair("password", "1205"));
        nameValuePairList.add(new BasicNameValuePair("language", "en"));

        try {
            HttpClient mHttpClient = new DefaultHttpClient();
            HttpPost mHttpPost = new HttpPost(PATH);
            mHttpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList));

            HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
            HttpEntity mHttpEntity = mHttpResponse.getEntity();

            StatusLine mStatusLine = mHttpResponse.getStatusLine();

            int statusCodeString = mStatusLine.getStatusCode();

            if (statusCodeString == 200) {

                mInputStream = mHttpEntity.getContent();

                mStringBuilder = new StringBuilder();

                BufferedReader mBufferedReader = new BufferedReader(
                        new InputStreamReader(mInputStream, "iso-8859-1"), 8);

                while ((lineString = mBufferedReader.readLine()) != null)
                    mStringBuilder.append(lineString + "/n");

            }

            resultString = mStringBuilder.toString();

        } catch (Exception e) {
            // TODO: handle exception
        }
        return resultString;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }


我得到一个空的BufferedReader.ReadLine()。我是否正确创建mHttpPost
我也尝试了HTTPget方法。
但结果是一样的。请帮帮我

我已经在Browser.Check上检查了我的PHP脚本。我的PHP脚本运行正常。

最佳答案

您需要将键值对转换为内容类型application / json,然后将其发送到服务器。

在asynctask中尝试一下,

JSONObject data = new JSONObject();

// Add key/value pairs
data.put("email", email);
data.put("login", login);
data.put("password", password);
data.put("language", language);

JSONObject json = new JSONObject();
json.put("command", command);
json.put("data", data);

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost("http://alphabravo.com/myapp/api.php");

StringEntity se;
se = new StringEntity(json.toString());

// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");


HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);

HttpEntity entity = response.getEntity();

String receiveJson = EntityUtils.toString(entity);

//use receiveJson as new JSONObject(receiveJson) ot new JSONArray(receiveJson) as per your returned json value

关于java - Android JSON通讯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19287308/

相关文章:

java - 检查数组中的特定整数

java - Java-Cassandra在“IN”中带有大量参数

php - 在oscommerce中的类别下显示子类别

java.lang.IllegalStateException : The content of the adapter has changed but HederListView did not receive a notification

java - 来自 PHP 的问题 - 使用 @RequestParameter 参数的 Spring 3 REST PUT/POST 方法

java - AnnotationException : A Foreign key refering has the wrong number of column. 应该是 0

php - 在 Yii2 的 SqlDataProvider 中使用 SQL_CALC_FOUND_ROWS

php - Laravel 在 foreach 中将数据从一个模型发送到另一个模型

java - Android/Java - 将等式中的 int 值转换为 double 值

android - 如何从 map 中删除所有折线