0

My android project I want to send json data (As Arraylist),with header..

and read server response as string..

DefaultHttpClient is deprecated..So,I am using HttpURLConnection..

here is my code:-

@Override public String Signup(String name, String phno) throws Exception { String url="http://address/AndroidProject/userSignup"; // Instantiate and initialise a SignupForm Form suf = new Form(name, phno); // store suf in a list List<NameValuePair> pairs = new ArrayList<NameValuePair>(1); // create the K,V to be sent pairs.add(new BasicNameValuePair("suf", new Gson().toJson(suf))); try { URL url1 = new URL(url); HttpURLConnection connection = (HttpURLConnection) url1.openConnection(); //encoded pairs HttpEntity requestEntity = new UrlEncodedFormEntity(pairs); connection.setRequestMethod("POST"); //add header connection.setRequestProperty("Id", myId); connection.setDoOutput(true); //send data OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(requestEntity.toString()); writer.flush(); connection.connect(); // read the output from the server String content = connection.getResponseMessage(); int responseCode = connection.getResponseCode(); // if response HttpStatus is not 'CREATED' if (responseCode != 201) { // throw the error message to the application } // we can return the authToken. return content; } catch (Exception e) { e.printStackTrace(); throw e; } } 

I am getting error:

Server response string: Required String parameter 'suf' is not present Signup access server error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 

But this code not sending any data to server..where is the problem??Why I cannot post value??what I missed?

6
  • what error you are getting Commented Jan 16, 2016 at 5:40
  • Have u declare permission in manifest file...<uses-permission android:name="android.permission.INTERNET" /> Commented Jan 16, 2016 at 5:41
  • @NIK, yes I have added Commented Jan 16, 2016 at 5:49
  • @preeti, basicnamevaluepair is also deprecated Commented Jan 16, 2016 at 6:02
  • try this, stackoverflow.com/questions/34072845/post-data-using-volley-php/… Commented Jan 16, 2016 at 6:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.