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:
HTTPPoster.java
upload.php (on the server)
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);
?>
