Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send data to php?
    primarykey
    data
    text
    <p>I have a code for reading a Json from a php and it works, but it only reads a JSON from a PHP, I need to send some data from android to php and then read it obtaining the requested data.</p> <p>Can someone help me including that stuff in to that code?</p> <p>PHP/JSON READING WORKING CODE: I need to implement the sending over there but I don't know where start</p> <pre><code>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&lt;String, Void, JSONObject&gt; { 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 &lt; 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("&lt;br /&gt;"); 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(); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload