• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Sending JSON to server

lintwurm

Lurker
Hello everyone,

I am new to the forum and have struggled with this app for a couple of days now. This is only the 2nd app I am writing and what I want to do is send a JSON string to my server and have the server just write it to a text file (for now).

The problem lies in the HTTPPoster class as far as I know. Any help would be GREATLY appreciated.

JSON.java:
Code:
package example.JSON;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class JSON extends Activity
{
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
		JSONObject json = new JSONObject();
		try
		{
			json.put("Longtude", "15");
			json.put("Lattetude", "30");
		}
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String url = "http://www.upgardens.co.za/upload.php";
		HttpResponse re;
		String temp = new String();
		try
		{
			Toast.makeText(this, "Going to HTTPPoster class now... Let's hope I don't die!", Toast.LENGTH_LONG).show();
			re = HTTPPoster.doPost(url, json);
			Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show();
			temp = EntityUtils.toString(re.getEntity());
		}
		catch (ClientProtocolException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (temp.compareTo("SUCCESS")==0)
		{
		    Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
		}
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    }
}

HTTPPoster.java
Code:
package example.JSON;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

public class HTTPPoster
{
	public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
	{
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost request = new HttpPost(url);
	    HttpEntity entity;
	    StringEntity s = new StringEntity(c.toString());
	    s.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
	    entity = s;
	    request.setEntity(entity);
	    HttpResponse response;
	    response = httpclient.execute(request);
	    return response;
	}
}

upload.php (on the server)
Code:
<?php
 
	// decode JSON string to PHP object
	$decoded = json_decode($_GET['json']);
	// do something with data here
	$Longitude = $decoded['Longitude'];
	$Lattetude = $decoded['Lattetude'];
	$myFile = "testFile.txt";
	$fh = fopen($myFile, 'w') or die("can't open file");
	$stringData = $Longitude;
	fwrite($fh, $stringData);
	$stringData = $Lattetude;
	fwrite($fh, $stringData);
	fclose($fh);
	// create response object
	$json = array();
	$json['errorsNum'] = 2;
	$json['error'] = array();
	$json['error'][] = 'Wrong Longtude!';
	$json['error'][] = 'Wrong Lattetude!';
	// encode array $json to JSON string
	$encoded = json_encode($json);
	// send response back to index.html
	// and end script execution
	die($encoded);
?>
 
Hi lintwurm,

What was exactly the problem? The php script unable to received the json that the android posted? is there error beign thrown? please do provide logs so it would easier for us to trace.

Have you tried using
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
instead of StringEntity?

Since you're using HttpPost it is much appropriate to use $_POST
$decoded = json_decode(stripslashes($_POST['json']));


Best,
 
Hi,

It seems like the php is never receiving the json string. as even if I take everything out and just put die("SUCCESS"); it still doesn't do anything. (if I use the die("SUCCESS"); message the program should run further.)

It seems like it breaks in the HTTPPoster.java class somewhere. I am very new to android application programming, so I don't know where to check the log files and I changed the php to include $decoded = json_decode(stripslashes($_POST['json'])); instead of the other way I used.
It still doesn't work.
I am not sure how to use the List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); that you suggested. (I will do a bit of reading though and let you know)

Thanks for your patience :D
 
Hey guys. Thanks for this tricks. It has been very helpful for me.
But i don't understand one thing. I'm using a JSONObject which is filled with two fields by put() method. One is an ArrayList<Integer> and the other one is a string "user" which I get from the bundle

final String user = b.getString("il dato inserito
 
Back
Top Bottom