【问题标题】:How to send data to php? [closed]如何将数据发送到php? [关闭]
【发布时间】:2013-09-19 01:15:38
【问题描述】:

我有一个从 php 读取 Json 的代码,它可以工作,但它只从 PHP 读取 JSON,我需要将一些数据从 android 发送到 php,然后读取它以获得请求的数据。

有人可以帮我将这些内容包含在该代码中吗?

PHP/JSON 读取工作代码: 我需要在那里实现发送,但我不知道从哪里开始

package com.json.php;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



import com.json.php.R;

public class JSONExampleActivity extends Activity {
Button send;
EditText txsms;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chatxml);
        TextView textView = (TextView) findViewById(R.id.textView1);
        EditText txsms = (EditText) findViewById(R.id.txsms);
        send= (Button) findViewById(R.id.send);

        send.setOnClickListener(new View.OnClickListener(){

            public void onClick(View view){
            TextView textView = (TextView) findViewById(R.id.textView1);
            EditText txsms = (EditText) findViewById(R.id.txsms);
            String a=txsms.getText().toString();

            textView.setText("mezu:"+a);
            xsend("luis", a,"opinion");

              new GetSmsTask().execute("http://iatek.eu/sys/getsms.php");                              
                }});       






        textView.setMovementMethod(new ScrollingMovementMethod());
        new GetSmsTask().execute("http://iatek.eu/sys/getsms.php");
       }   

    private class GetSmsTask extends AsyncTask<String, Void, JSONObject> {
     protected JSONObject doInBackground(String... urls) {
     JSONObject obj = null;
     try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(urls[0]);
            HttpResponse response = httpclient.execute(httppost);           
         /*   String jsonResult = "{\"posts\":" + //FOR TESTS
                    "[{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}," +
                    " {\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}, " +
                    "{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}," +
                    "{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}]}";                     

            */
            String jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); 
            obj = new JSONObject(jsonResult);
        } 
        catch (JSONException e) {
            e.printStackTrace();
        } 
        catch (ClientProtocolException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }

        return obj;

     }
     private StringBuilder inputStreamToString(InputStream is) {
         String rLine = "";
         StringBuilder answer = new StringBuilder();
         BufferedReader rd = new BufferedReader(new InputStreamReader(is));

         try {
          while ((rLine = rd.readLine()) != null) {
           answer.append(rLine);
            }
         }

         catch (IOException e) {
             e.printStackTrace();
          }
         return answer;
        }

     protected void onPostExecute(JSONObject obj) {
      JSONArray jsonArray;
  String dana = null;
    try {
         jsonArray = obj.getJSONArray("posts");
        for (int i = 0; i < jsonArray.length(); i++) 
        {



              JSONObject childJSONObject = jsonArray.getJSONObject(i);
                 String username = childJSONObject.getString("username");
                 String sms = childJSONObject.getString("sms");
                 String fcat = childJSONObject.getString("fcat");
                 dana=dana+ " "+ ""+username+":" + sms +Html.fromHtml("<br />");
                 TextView textView = (TextView) findViewById(R.id.textView1);
                textView.setText(""+dana);
                textView.scrollTo(0, textView.getLineHeight()*(textView.getLineCount()-11));

        }


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


     }
 }


}

【问题讨论】:

    标签: java php android json request


    【解决方案1】:

    您可以通过发布请求或获取请求将数据发送到服务器。 查看此示例以将数据发布到服务器。

    http://androidexample.com/How_To_Make_HTTP_POST_Request_To_Server_-_Android_Example/index.php?view=article_discription&aid=64&aaid=89

    希望对你有帮助。

    【讨论】:

    • 对不起,但在该代码中没有请求数组,我也需要它。这个想法正在实现发送到我的代码,你能帮我吗?谢谢
    • 您可以通过将数组数据设为逗号分隔列表并将其放入 post 参数中来发送数组数据。
    • 你能给我发个例子吗?这个sintax是非常令人讨厌的:)
    【解决方案2】:

    尝试在 http 请求中使用参数:

    new GetSmsTask().execute("http://iatek.eu/sys/getsms.php"+"?"+arg1+"="+val1); 
    

    【讨论】:

    • 这行得通吗?真的!?我会试试的!
    • 无法运行,应用程序崩溃
    • 您的 php 代码必须在服务器端处理这些参数
    【解决方案3】:
    HttpClient httpclient = new DefaultHttpClient();
    String jsonString = "Some json string";
    HttpPost httppost = new HttpPost(urls[0]);
    httppost.setEntity(new StringEntity(jsonString, HTTP.UTF_8)); 
    

    【讨论】:

    • 但我需要发送参数名称和参数值,例如 parameter=0value""
    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 2014-04-27
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 2022-08-19
    相关资源
    最近更新 更多