Note that there are some explanatory texts on larger screens.

plurals
  1. POreading json from php web service in android
    primarykey
    data
    text
    <p>I am using a object I created in my android object to send values as json to my php web service. the sending works well. But after the send I am expected to collect the object back from the web service based on my design which I would now test to be sure that there was a successful post. But I get a funny message in my Log cat as an exception through in my catch statement. I just believe that it as to do with how I am compressing json from my php webservice to android java code.</p> <p>Exception below <strong>Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.</strong></p> <p>JSON OUTPUT 11-15 07:48:02.622: I/global(276): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 11-15 07:48:02.622: I/output(276): <br /> 11-15 07:48:02.622: I/output(276): <b>Warning</b>: Cannot modify header information - headers already sent by (output started at C:\wamp\www\Rhema\config\config.php:17) in <b>C:\wamp\www\Rhema\webservice\RegisterMember.php</b> on line <b>50</b><br /> 11-15 07:48:02.622: I/output(276): {"Name":"femi","Username":"dsasdfasft","Phone":"456346345345645","Email":"a@yahoo.com","Submit":"Submit"}</p> <pre><code>My java code below doing the sending //The first code class is used to represent the json object that would be passed to php package com.example.objects; public class MemberModel { private String Name; private String Username; private String Phone; private String Email; private String Submit; public void setName(String Name){ this.Name = Name; } public String getName(){ return Name; } public void setUsername(String Username){ this.Username = Username; } public String getUsername(){ return Username; } public void setPhone(String Phone){ this.Phone = Phone; } public String getPhone(){ return Phone; } public void setEmail(String Email){ this.Email = Email; } public String getEmail(){ return Email; } public void setSubmit(String Submit){ this.Submit = Submit; } public String getSubmit(){ return Submit; } //This is going to be used to set the json string notation public final static String Member_Name = "Name"; public final static String Member_Username = "Username"; public final static String Member_Phone = "Phone"; public final static String Member_Email = "Email"; public final static String Member_Submit = "Submit"; } //This is a snippet from the asynctask class that is sending the json object to php @Override protected String doInBackground(String... arg0) { // TODO Auto-generated method stub try{ JSONHttpClient jsonobject = new JSONHttpClient(); model = (MemberModel)jsonobject.PostObject(RestfulUrl.RegisterURL, model, MemberModel.class); Log.i("gothere","here"); if(model != null){ Log.i("return",model.getSubmit()); } else{ Log.i("return","wrong values"); } } catch(Exception e){ } return null; } //The snippet below is the main part of the json object that does the post //I have tested this with a asp.net mvc app server and it works prity well public &lt;T&gt; T PostObject(final String url, final T object, final Class&lt;T&gt; objectClass) { DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); try { StringEntity stringEntity = new StringEntity(new GsonBuilder().create().toJson(object)); Log.i("jsonobject",stringEntity.toString()); httpPost.setEntity(stringEntity); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); httpPost.setHeader("Accept-Encoding", "gzip"); HttpResponse httpResponse = defaultHttpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { InputStream inputStream = httpEntity.getContent(); Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding"); if (contentEncoding != null &amp;&amp; contentEncoding.getValue().equalsIgnoreCase("gzip")) { inputStream = new GZIPInputStream(inputStream); } String resultString = convertStreamToString(inputStream); inputStream.close(); return new GsonBuilder().create().fromJson(resultString, objectClass); } } catch (UnsupportedEncodingException e) { Log.i("a",e.toString()); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (ClientProtocolException e) { Log.i("b",e.toString()); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (IOException e) { Log.i("c",e.toString()); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } return null; } public &lt;T&gt; T PostParams(String url, final List&lt;NameValuePair&gt; params, final Class&lt;T&gt; objectClass) { String paramString = URLEncodedUtils.format(params, "utf-8"); url += "?" + paramString; return PostObject(url, null, objectClass); } private String convertStreamToString(InputStream inputStream) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line = null; try { while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line + "\n"); } } catch (IOException e) { Log.i("first",e.toString()); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } finally { try { inputStream.close(); } catch (IOException e) { Log.i("second",e.toString()); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } return stringBuilder.toString(); } //My php code below function __construct(){ $this-&gt;connection = new Connector(); $this-&gt;connection-&gt;doConnection(); $json = file_get_contents('php://input'); $obj = json_decode($json); if(isset($obj)) $this-&gt;register($obj); } function register($obj){ $name = $obj-&gt;{"Name"}; $username = $obj-&gt;{"Username"}; $phone = $obj-&gt;{"Phone"}; $email = $obj-&gt;{"Email"}; $query = "insert into member (name, username, phone, email,rhemabranchid ) values ('$name','$username','$phone','$email',1)"; $result = mysql_query($query) or die(mysql_error()); $id = mysql_insert_id(); mysql_close(); if($id &gt;0){ // echo "successful"; $array = array("Name"=&gt;$name,"Username"=&gt;$username,"Phone"=&gt;$phone,"Email"=&gt;$email,"Submit"=&gt;"Submit"); header('Content-type: application/json'); echo json_encode($array); } else echo "failed"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload