【问题标题】:Android how to send data string via Json to server?Android如何通过Json将数据字符串发送到服务器?
【发布时间】:2014-04-25 17:02:55
【问题描述】:

我需要在我的 Android 应用程序中发送一些由 edittext 输入的数据,但我不明白我必须如何为服务器端编写代码!我在 PHP 5.3 上。

【问题讨论】:

标签: android server android-json


【解决方案1】:

您可以使用查询字符串来完成。但为此,您需要一个用于服务器端的 api。 要在服务器端发送数据,您可以使用以下代码:-

public String connect(String url) 
    {//for connect to database(QueryString API)
        String userID = null;
        HttpClient httpc = new DefaultHttpClient();
        try 
        {
        HttpPost post = new HttpPost(url);

            HttpResponse response = httpc.execute(post);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
            {
                userID = EntityUtils.toString(response.getEntity());
                Log.v("Login response", "" + userID);
            }
        }   
        catch (ClientProtocolException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        return userID;
    }

public void abcd(String a1, String a2, String a3,
            String a4, String a5) {

        String QS ="";//acc. to ur data
        String finalURL2 = url + QS;

        String result2 = connect(finalURL2);
        Log.v("message", result2);
    }

【讨论】:

    【解决方案2】:
    it Depends how your server side is implemented Normally sending data Requires Either HTTPPost Method or HTTPPut methods. More commonly used is HTTPPost methods That has header data and Body Data
    
    you need to do it in following way
    
    1- convert your json object to String
    
    user.toString();
    2- add Targeted URL
    
    String URL="Enter URL here";
    3- add url to the request
    
    response = dohttpPostWithCode(URL.toString(),user.toString());
    response is String [ ] that have 2 index i- for response code ii- for data
    
    4- method to handle data
    
     public String[] dohttpPostWithCode(String url,String postParameters ) throws Exception {
             URL weburl = new URL(url);
             URI uri = new URI(weburl.getProtocol(), weburl.getUserInfo(), weburl.getHost(), weburl.getPort(), weburl.getPath(), weburl.getQuery(), weburl.getRef());
                BufferedReader in = null;
                String[] result = new String[2];         
                try {
                     HttpParams httpParameters = new BasicHttpParams();
                    // Set the timeout in milliseconds until a connection is established.
                    int timeoutConnection = 20000;
                    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                    // Set the default socket timeout (SO_TIMEOUT) 
                    // in milliseconds which is the timeout for waiting for data.
                    int timeoutSocket = 20000;
                    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
                    HttpURLConnection httpURL=(HttpURLConnection) weburl.openConnection();
                    httpURL.setDoOutput(true);
                    httpURL.setDoInput(true);
                    httpURL.setRequestMethod("POST");
                    HttpClient client =new  DefaultHttpClient(httpParameters);            
                    HttpPost httpPost = new HttpPost(uri);
                    //httpPost.addHeader("language","en");
                    httpPost.addHeader("Content-Type",  "application/json");
    
                   // StringEntity entity = new StringEntity(postParameters, HTTP.UTF_8);
                    httpPost.setEntity(new StringEntity(postParameters));
                   // httpPost.setEntity(entity);
                   // httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
                    HttpResponse response = client.execute(httpPost);               
                    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    StringBuffer sb = new StringBuffer("");
                    String line = "";
                    String NL = System.getProperty("line.separator");
    
                    while ((line = in.readLine()) != null) {
                        sb.append(line + NL);
                    }
                    in.close();
    /*              String result = sb.toString();
                    return result;*/
                    result[0] = response.getStatusLine().getStatusCode()+"";
                    result[1] = sb.toString();
                    return result;
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 2013-12-06
      • 2021-11-20
      • 1970-01-01
      • 2011-09-16
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多