2013-12-11 3 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

우선이 브라우저에서 전화를 걸어 응답을 확인하십시오. 크롬 PostMan 나머지 클라이언트에 확장 이 있습니다. 먼저 확인하십시오. 응답을 제대로받지 못하고있는 것일 수 있습니다.

관련 문제