2012-07-23 64 views
1

你好人我试图把我的JSONArray发送到PHP页面 我得到HTTP // 1.1 200,但我仍然无法显示它在我的PHP页面,我仍然没有知道我必须成功,这里给它或不 是我的Android代码Android - 发送JSONArray到php

public void sendJson() { 

       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
       HttpResponse response; 

       try{ 
        HttpPost post = new HttpPost("http://sap-sp-test.dff.jp/sp.php/ranking/index"); 
        Log.i("entry", MixiActivity.entry.toString()); 

        ArrayList<NameValuePair> nVP = new ArrayList<NameValuePair>(2); 
        nVP.add(new BasicNameValuePair("json", MixiActivity.entry.toString())); 
        post.setEntity(new UrlEncodedFormEntity(nVP)); 
        response = client.execute(post); 
        Log.i("postData", response.getStatusLine().toString()); 
       } 
       catch(Exception e){ 
        e.printStackTrace(); 
        Log.i("error", e.getMessage()); 
       } 

    } 

PHP代码

$data = $_POST['json']; 
echo $data; 

我已经尝试了一些例子在这里,但我仍然无法发送的数据 和JSON数组(MixiActivity.entry)不为空 ,当我尝试使用日志打印,日志显示JSON阵列 权价值,但我不能将其发送到PHP 请帮我解决这个问题

非常感谢

尼科

回答

1

最后,我放弃了在安卓 HTTP POST现在,我使用JavaScript来得到Android的

与此jsonarray

公共类JavaScriptInterface {0}上下文mContext;

  JavaScriptInterface(Context c) { 
      mContext = c; 
     } 

     public String load() { 
      String JS = JsonArray.toString(); 
      return JS; 
     } } 

,并在HTML

function loadJSON()   
{   
var jsonData = Android.load(); 
var myDictionary = [];   
myDictionary["jsonData"] = jsonData; 

post(myDictionary, "http://sap-sp-test.dff.jp/sp.php/ranking/index?user_id=" + user_id+"&display_name=" + display_name + "&photo=" + photo +"&device=android", "post");   
} 

为后到PHP我使用表单POST在JavaScript

function post(dictionary, url, method) 
     { 
      method = method || "post"; // post (set to default) or get 

      // Create the form object 
      var form = document.createElement("form"); 
      form.setAttribute("method", method); 
      form.setAttribute("action", url); 

      // For each key-value pair 
      for (key in dictionary) { 
       //alert('key: ' + key + ', value:' + dictionary[key]); // debug 
       var hiddenField = document.createElement("input"); 
       hiddenField.setAttribute("type", "hidden"); // 'hidden' is the less annoying html data control 
       hiddenField.setAttribute("name", key); 
       hiddenField.setAttribute("value", dictionary[key]); 
       form.appendChild(hiddenField); // append the newly created control to the form 
      } 

      document.body.appendChild(form); // inject the form object into the body section 
      form.submit(); 
     } 

,并在PHP,用于接收JSON

if($this->_getParam('device') == "android") 
     { 
      $data = $_POST["jsonData"]; 
      $data = stripslashes($data); 
      $friends = $data; 
     } 

此方法适用于我 无论如何感谢mariachi你的帮助

2

你可以尝试添加编码在post.setEntity()这样的...

post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8")); 

每一件事情别人看起来好像没什么问题。如果不工作,那么你也可以尝试使用JSON库在android系统直接进行编码,并将数据传递到在PHP中使用

HttpPost httppost = new HttpPost("http://your/url/path/"); 
httppost.addHeader("Content-Type", "application/json"); 
httppost.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8"))); 

然后页面,您可以通过这个代码访问它

$json = file_get_contents('php://input'); 
$obj = json_decode($json); 
$name = $obj->{'name'}; 

或者

您也可以尝试做一个GET请求或在你的PHP文件访问通过$ _REQUEST数据。 无论您使用的是什么,都是必需的。

试试这个,这是一个工作代码...这是你可以直接使用将数据传递到您的文件中的类。静态调用使用::功能,并导入所有必需的JSON clases

public class RestClient { 

private static String convertStreamToString(InputStream is) { 
    /* 
    * To convert the InputStream to String we use the BufferedReader.readLine() 
    * method. We iterate until the BufferedReader return null which means 
    * there's no more data to read. Each line will appended to a StringBuilder 
    * and returned as String. 
    */ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    return sb.toString(); 
} 




public static String connectSend(){ 

    /* 
    HttpParams httpparams = new BasicHttpParams(); 
    HttpConnectionParams.setConnectionTimeout(httpparams, 30000); 
    HttpConnectionParams.setSoTimeout(httpparams, 30000); 
    */ 

    HttpClient httpclient = new DefaultHttpClient(); 

    HttpPost httppost = new HttpPost("http://path/to/your/php/file"); 

    httppost.addHeader("Content-Type", "application/json"); 

    JSONObject json = new JSONObject(); 
    try { 
     json.put("item0", "data0"); 
     json.put("item1", "data1"); 
     json.put("item2", "data2"); 
     json.put("item3", "data3"); 
    } catch (JSONException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 


    try { 
     httppost.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8"))); 
     //httppost.setHeader("json", json.toString()); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream instream = entity.getContent(); 
      String result = RestClient.convertStreamToString(instream); 


      return result; 
     } 


    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return e.toString(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return e.toString(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return e.toString(); 
    } 
    catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return "false"; 

} 
+0

确定我会尝试它感谢mariachi!我会告诉你结果 – 2012-07-23 09:43:14

+0

对不起mariachi我已经尝试过但我仍然不能发布JSON数组任何其他想法? – 2012-07-23 10:17:27

+0

你对response.getStatusLine()。toString()的回应是什么,你试过我的哪一个建议? – 2012-07-23 10:36:37