2013-12-11 140 views
0

我想将这些值存储到服务器。 我必须存储的数据在变量中是完整的。 我应该得到JSON对象返回-1

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

从服务器 但得到-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; 
} 

我无法找到我的错误!

谢谢

+1

请阅读:http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call –

+0

不知道我看到该链接的相关性。这里发布的代码不是异步请求,而是完全同步 - 它将等待服务器响应(在服务器完成发送数据之前,'linkinStream.read()'方法调用将不返回-1)。我不确定问题是什么,但它似乎并不是你所指的。 – Jules

回答

0

首先尝试从浏览器进行此调用并检查响应。在Chrome PostMan休息客户端上有一个扩展 。请先检查。可能是你没有得到正确的答复。