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?

user_apr
  • 719
  • 2
  • 10
  • 27

0 Answers0