2013-12-11 167 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休息客戶端上有一個擴展 。請先檢查。可能是你沒有得到正確的答覆。