如何发送数据到PHP? - java

Improve this question

我有一个用于从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();
    }


     }
 }


}

参考方案

HttpClient httpclient = new DefaultHttpClient();
String jsonString = "Some json string";
HttpPost httppost = new HttpPost(urls[0]);
httppost.setEntity(new StringEntity(jsonString, HTTP.UTF_8)); 

Android:如何为wrap_content设置高度动画? - java

我需要使用ValueAnimator来使用户拖动特定视图时出现自定义“放置字段”。 (我想将字段从gone, height = 0更改为visible, height = wrap_content)。我已经尝试过以下问题的解决方案:How to animate to wrap_content?当我在单个TextView上使用它时,答案就起作用了,但是当我尝试…

Java-搜索字符串数组中的字符串 - java

在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…

为什么我的应用在启动时总是崩溃-Android - java

刚刚遵循了有关如何制作按钮和活动的youtube指南。我按照他的代码减小字体大小,并且在启动时一直崩溃。有人知道为什么吗?public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { su…

Java Double与BigDecimal - java

我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…

不需要选择回收站查看项目 - java

viewHolder.optiontxt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewHolder.optiontxt1.setCompoundDrawablesWithIntrinsicBounds(R.dra…