1. Download our Official Android App: Forums for Android!

Apps Sending JSON to server

Discussion in 'Android Development' started by lintwurm, Mar 15, 2011.

  1. lintwurm

    lintwurm Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Mar 15, 2011

    Mar 15, 2011
    3
    0
    5
    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 (Text):
    1.  
    2. package example.JSON;
    3.  
    4. import java.io.IOException;
    5.  
    6. import org.apache.http.HttpResponse;
    7. import org.apache.http.client.ClientProtocolException;
    8. import org.apache.http.util.EntityUtils;
    9. import org.json.JSONException;
    10. import org.json.JSONObject;
    11.  
    12. import android.app.Activity;
    13. import android.os.Bundle;
    14. import android.widget.Toast;
    15.  
    16. public class JSON extends Activity
    17. {
    18.     /** Called when the activity is first created. */
    19.     @Override
    20.     public void onCreate(Bundle savedInstanceState)
    21.     {
    22.         JSONObject json = new JSONObject();
    23.         try
    24.         {
    25.             json.put("Longtude", "15");
    26.             json.put("Lattetude", "30");
    27.         }
    28.         catch (JSONException e)
    29.         {
    30.             // TODO Auto-generated catch block
    31.             e.printStackTrace();
    32.         }
    33.         String url = "http://www.upgardens.co.za/upload.php";
    34.         HttpResponse re;
    35.         String temp = new String();
    36.         try
    37.         {
    38.             Toast.makeText(this, "Going to HTTPPoster class now... Let's hope I don't die!", Toast.LENGTH_LONG).show();
    39.             re = HTTPPoster.doPost(url, json);
    40.             Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show();
    41.             temp = EntityUtils.toString(re.getEntity());
    42.         }
    43.         catch (ClientProtocolException e)
    44.         {
    45.             // TODO Auto-generated catch block
    46.             e.printStackTrace();
    47.         }
    48.         catch (IOException e)
    49.         {
    50.             // TODO Auto-generated catch block
    51.             e.printStackTrace();
    52.         }
    53.         if (temp.compareTo("SUCCESS")==0)
    54.         {
    55.             Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
    56.         }
    57.         super.onCreate(savedInstanceState);
    58.         setContentView(R.layout.main);
    59.     }
    60. }
    61.  
    HTTPPoster.java
    Code (Text):
    1.  
    2. package example.JSON;
    3.  
    4. import java.io.IOException;
    5.  
    6. import org.apache.http.Header;
    7. import org.apache.http.HttpEntity;
    8. import org.apache.http.HttpResponse;
    9. import org.apache.http.client.ClientProtocolException;
    10. import org.apache.http.client.HttpClient;
    11. import org.apache.http.client.methods.HttpPost;
    12. import org.apache.http.entity.StringEntity;
    13. import org.apache.http.impl.client.DefaultHttpClient;
    14. import org.apache.http.message.BasicHeader;
    15. import org.apache.http.protocol.HTTP;
    16. import org.json.JSONObject;
    17.  
    18. public class HTTPPoster
    19. {
    20.     public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException
    21.     {
    22.         HttpClient httpclient = new DefaultHttpClient();
    23.         HttpPost request = new HttpPost(url);
    24.         HttpEntity entity;
    25.         StringEntity s = new StringEntity(c.toString());
    26.         s.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    27.         entity = s;
    28.         request.setEntity(entity);
    29.         HttpResponse response;
    30.         response = httpclient.execute(request);
    31.         return response;
    32.     }
    33. }
    34.  
    upload.php (on the server)
    Code (Text):
    1.  
    2. <?php
    3.  
    4.     // decode JSON string to PHP object
    5.     $decoded = json_decode($_GET['json']);
    6.     // do something with data here
    7.     $Longitude = $decoded['Longitude'];
    8.     $Lattetude = $decoded['Lattetude'];
    9.     $myFile = "testFile.txt";
    10.     $fh = fopen($myFile, 'w') or die("can't open file");
    11.     $stringData = $Longitude;
    12.     fwrite($fh, $stringData);
    13.     $stringData = $Lattetude;
    14.     fwrite($fh, $stringData);
    15.     fclose($fh);
    16.     // create response object
    17.     $json = array();
    18.     $json['errorsNum'] = 2;
    19.     $json['error'] = array();
    20.     $json['error'][] = 'Wrong Longtude!';
    21.     $json['error'][] = 'Wrong Lattetude!';
    22.     // encode array $json to JSON string
    23.     $encoded = json_encode($json);
    24.     // send response back to index.html
    25.     // and end script execution
    26.     die($encoded);
    27. ?>
    28.  
     

    Advertisement

  2. ruelblapid

    ruelblapid Lurker
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Mar 14, 2011

    Mar 14, 2011
    2
    0
    5
    Android Engineer at Airpush.com
    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,
     
  3. lintwurm

    lintwurm Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Mar 15, 2011

    Mar 15, 2011
    3
    0
    5
    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
     
  4. lintwurm

    lintwurm Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Mar 15, 2011

    Mar 15, 2011
    3
    0
    5
    I gave up on the JSON idea and only use a http post instead...
    It finally works!

    Thanks for the help and tips
     
  5. Stell4r

    Stell4r Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Jul 27, 2011

    Jul 27, 2011
    1
    0
    5
    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
     

Share This Page

Loading...