0

I want to store these values to server. The data which i have to store is complete in the variables. I should get

    {"response":{"result":1,"Message":"Poll Entered Successfully."}}

from server but getting -1

@Override
protected String doInBackground(String... params) {
String jsonString = null;
HttpURLConnection linkConnection = null;
try {

    String squestion = question.getText().toString();
    String shashtag = hashTag.getText().toString();
    spolltime = polltime.getText().toString();

    StringBuilder scat = new StringBuilder();
    scat.append("");
    scat.append(categoryid);

    StringBuilder sid = new StringBuilder();
    sid.append("");
    sid.append(LoginPage.logid);
    //int ipolltime = Integer.parseInt(spolltime);
    String equestion = URLEncoder.encode(squestion,"UTF-8");
    String ehashtag = URLEncoder.encode(shashtag,"UTF-8");
    String  ecategory = URLEncoder.encode(scat.toString(),"UTF-8");
    String  eA = URLEncoder.encode(OptionA.stext,"UTF-8");
    String  eB = URLEncoder.encode(OptionB.stext,"UTF-8");
    String  eC = URLEncoder.encode(OptionC.stext,"UTF-8");
    String  eD = URLEncoder.encode(OptionD.stext,"UTF-8");
    String  eid = URLEncoder.encode(sid.toString(),"UTF-8");
    String  epolltime = URLEncoder.encode(spolltime,"UTF-8");

    URL linkurl = new URL("http://iipacademy.in/askpoll/pollfeeds.php?user_id="+eid+"&question="+equestion+
            "&hashtag="+ehashtag+"&category_id="+ecategory+"&option1="+eA+"&option2="+eB+"&option3="+eC+"&option4="+eD+"" +
                    "&pollopen="+epolltime+"&optiontype1=texty&optiontype2=texty&optiontype3=texty&optiontype4=texty");

    linkConnection = (HttpURLConnection) linkurl.openConnection();                                                  
    int responseCode = linkConnection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        InputStream linkinStream = linkConnection.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int j = 0;
        while ((j = linkinStream.read()) != -1) {
            baos.write(j);
        }
        byte[] data = baos.toByteArray();
        jsonString = new String(data);
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (linkConnection != null) {
        linkConnection.disconnect();
    }
}

//Toast.makeText(getApplicationContext(),jsonString.toString(),
    //Toast.LENGTH_LONG).show();
return jsonString;
}

I am not able to find my mistake !

thanks

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
  • 1
    Please read: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – John Dvorak Dec 11 '13 at 07:12
  • Not sure I see the relevance of that link. The code posted here is not an asynchronous request, but completely synchronous -- it will wait for the server response (the 'linkinStream.read()' method call will not return -1 until the server has finished sending the data). I'm not sure what the problem is, but it doesn't seem to be what you're indicating. – Jules Dec 11 '13 at 07:39

1 Answers1

0

first of all try to make this call from browser and check for response . there is an extension on chrome PostMan rest client. please check first. may be you are not getting the response correctly.

nitesh goel
  • 6,338
  • 2
  • 29
  • 38