0

I've been trying this for the best part of two weeks now, and I am really stuck. Initially I had created a simple ObjectOutputStream client - server program - with the client being the Android app, but it does not work (it reads the connection but not the object).

So now I am confused as to what other approaches I might be able to take to carry out this simple task? Can anyone Help?

1
  • Are there any requirements on the server? Otherwise I'd consider going for an HTTP server and use that protocol to send data back and forth. Commented Feb 23, 2012 at 16:01

4 Answers 4

0

have you tried URLConnection using post method? :)

Or get method like:

String yourURL = "www.yourwebserver.com?value1=one&value2=two"; URL url = new URL(yourURL); URLConnection connection = url.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); response = in.readLine(); 
Sign up to request clarification or add additional context in comments.

Comments

0

you can try JSON stirng to send data. We have a lot of stuff available on how to work with JSON and also there are many api's. JSONSimple is the one I can suggest. Its really easy.

2 Comments

I have been looking at JSON but have failed to find a tutorial that I can understand, they all seem really convoluted.
If you want to serialize a java object to json. I'd suggest Gson sites.google.com/site/gson/gson-user-guide. This is really good, especially when deserializing from json to java object.
0

why don't you try this:

HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); 

3 Comments

also I recomend Json for parsing data
So I can post an Object(name value pairs) to a server and read it from a servlets? Can this work a with a hash table?
If your object data just have a couple of fields such as ProductID, Price, then, I think you can send them as a key value pairs. However, if you have a lot of fields, I believe using json and let the library take care reconstruct the object on server for you. :)
0

You can use this to post an Entity to server:

 HttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(url); postRequest.setEntity(entity); try { HttpResponse response = httpClient.execute(postRequest ); String jsonString = EntityUtils.toString(response .getEntity()); Log.v(ProgramConstants.TAG, "after uploading file " + jsonString); return jsonString; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

An Entity can be name value pair:

List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("key1", value1)); nvps.add(new BasicNameValuePair("key2", value2)); Entity entity=new UrlEncodedFormEntity(nvps, HTTP.UTF_8) 

Or you can send an entity with bytearray.

Bitmap bitmapOrg=getBitmapResource(); ByteArrayOutputStream bao = new ByteArrayOutputStream(); bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao); byte[] data = bao.toByteArray(); MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE) entity.addPart("file", new ByteArrayBody(data, "image/jpeg", "file")); 

If you want to post json to server:
Please check out this link How do I send JSon as BODY In a POST request to server from an Android application?
For serializing and deserializing java object, I recommend https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson Really hope it can help you see an overview of sending data to server

2 Comments

thanks i am going to look into posts to servers and attempt to write a servlet. Can a hash table be an entity also?
Yes, you can build an NameValuePair object from your hashtable. Then construct UrlEncodedFormEntity with that NameValuePair object

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.