Note that there are some explanatory texts on larger screens.

plurals
  1. POPosting data from android to PHP
    primarykey
    data
    text
    <p>I'm using this forum for a long time, and first of all, I would like to say thanx for helping me LOTS of times. This is the first time that I don't find an answer, which makes me CRAAZZZY...</p> <p>I'm trying to write a super simple app, which get/post data from/into a DB. the problem is, I can't post data (get is fine). I'm using a php file, which should get the data from the Android device, and send it to the DB. For a start, I can't post it to my php file. I've tried it ALL (that I know of).</p> <p>the PHP file-</p> <pre><code>$test=$_POST['v']; print $_POST['v']; </code></pre> <p>The Java file-</p> <p>public void postData() throws ClientProtocolException, IOException {</p> <pre><code> // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost post = new HttpPost("https://www.myweb.coom/test.php"); try { // Add your data List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(1); nameValuePairs.add(new BasicNameValuePair("v", "123")); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request httpclient.execute(post); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } } </code></pre> <p>when I put Log.* it shows me that everthing is ok. it seems that the PHP doesn't get any data... </p> <p>EDITING ASYNTASK----- public class Upload extends AsyncTask{</p> <pre><code>@Override protected String doInBackground(String... params) { try { JSONObject json = new JSONObject(); json.put("UserName", "test2"); json.put("FullName", "1234567"); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 10000); HttpConnectionParams.setSoTimeout(httpParams, 10000); HttpClient client = new DefaultHttpClient(httpParams); // String url = "http://my WAMP server/test.php"+ "json={\"UserName\":1,\"FullName\":2}"; HttpPost request = new HttpPost(url); request.setEntity(new ByteArrayEntity(json.toString().getBytes( "UTF8"))); request.setHeader("json", json.toString()); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need if (entity != null) { InputStream instream = entity.getContent(); String result = "gr8 Success"; Log.i("Read from server", result); } } catch (Throwable t) { Log.e("Request failed: " ,"ERR"+ t.toString()); } } </code></pre> <p>}</p> <p>MainActivity-</p> <pre><code>public void postData(View v) { //starts on button click if(upload!=null){ if(upload.getStatus()!= AsyncTask.Status.FINISHED){ Log.d("postData","no need to start AsyncTask"); return; } } upload= new Upload(); upload.execute(); Log.i("Async", "Starting..."); } </code></pre> <p>PHP- $json=$_GET ['json']; $obj = json_decode($json);</p> <pre><code> $posts = array(1); header('Content-type: application/json'); echo json_encode(array('posts'=&gt;$posts)); </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.
 

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