【问题标题】:post and get result in json from php file on android从android上的php文件发布并获取json结果
【发布时间】:2015-03-31 17:00:27
【问题描述】:

我是安卓新手。我搜索了很多,发现了很多代码,但我仍然有问题。

我有一个简单的 php,例如:(它有 两个 变量,例如 qnumber

http://domain.tld/post.php

并且将数据发布到这个页面后,会有一个JSON格式的结果。

我想在 php 页面上获得该输出,并在 android 上显示该值(我的意思是我希望在 eclipse 中的变量上拥有该值)

我已经检查了这段代码:

public void postData() {
     // Create a new HttpClient and Post Header
     HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
} 

但是我不能用这个得到输出。

【问题讨论】:

  • @IndraKumarS 我说。当我发送数据时,我无法获得该页面的 json 输出。焦点关闭:(
  • 你能发布你的 json 内容吗?
  • json 没有发送,我有两个字符串发送到该页面,它返回一个 JSON,那么如何在 android 中获取该输出?
  • 你如何测试?在设备中还是在模拟器中?....您是从 php 获取 json 数据吗?
  • 在设备上,也已经激活了用户许可。我在我的电脑上从 php 获取 JSON。

标签: php android eclipse methods


【解决方案1】:

您必须使用 Json 解析器。请找到处理对您的服务器的 get/post 请求的 ServiceHandler。

ServiceHandler.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
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.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;

import android.util.Log;

public class ServiceHandler {

    static InputStream is = null;
    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;

    public ServiceHandler() {

    }

    /*
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url, int method) {
        return this.makeServiceCall(url, method, null);
    }

    /*
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public String makeServiceCall(String url, int method,
            List<NameValuePair> params) {
        try {
            // http client

            HttpParams paramsH = new BasicHttpParams();
            paramsH.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            HttpClient httpClient = new DefaultHttpClient(paramsH);

            //DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);

            }
            httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            response = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error: " + e.toString());
        }

        return response;

    }
}

在youractivity.java中,这样调用

String resulting_json = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
ServiceHandler jsonParser = new ServiceHandler();
String URL = "http://domain.tld/post.php";
resulting_json = jsonParser.makeServiceCall(URL, ServiceHandler.POST, params);
Toast.makeText(youractivity.this, resulting_json, Toast.LENGTH_LONG).show();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多