I've been having a couple of problems coding this android application. What I want it to do is send a json object to a server. I have some code, was just wondering if someone can help me and tell me where I have gone wrong?
First is the JSON.java File:
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
{
re = HTTPPoster.doPost(url, json);
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);
}
}
Now HTTPPoster.java
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;
}
}
and now the php script on the server
upload.php
<?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);
?>
I should also say in the first code segment where I call: re = HTTPPoster.doPost(url, json);
is where the problem is...
The class I created is doing something funky...
Thanks upfront for any help
Edited by Roger, 23 July 2011 - 02:43 PM.
added prefix


Sign In
Create Account


Back to top









