2015-09-03 90 views
0

我想通過打開的連接發送用戶信息到服務器,它返回JSON中的用戶ID。一切都還好,但是當我試圖用getInputStream讀取用戶ID時,我無法讀取它(我帶500服務器錯誤)。當我用getErrorStream嘗試它並記錄結果時,JSON就來到了這裏。爲什麼?我如何克服它?安卓到服務器500錯誤

public String bilgiyigonder() { 
     HttpURLConnection connection = null; 
     try{ 
      Log.i("tago" ,"Veri Tabani bilgiyi gonder" + is.trim()); 
      Log.i("tago" , "Veri Tabani bilgiyi gonder" + urrl); 
      Log.i("tago" , "VeriTabani bilgiyi gonder" + longi); 
      Log.i("tago" ,"VeriTabani bilgiyi gonder" + lat); 
      connection = (HttpURLConnection)new URL("http://185.22.184.103/project/connection.php?name="+URLEncoder.encode("Faarık Fazıl", "ISO-8859-9")+"&url="+urrl+"&long="+longi+"&lat="+lat).openConnection(); 
      Log.i("tago" ,"VeriTabani bagı kurdum"); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 
     connection.setDoOutput(true); 
     connection.setDoInput(true); 
     connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible) "); 
     connection.setRequestProperty("Accept", "*/*"); 
     connection.setRequestProperty("Accept-Charset", charset); 
     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset); 

     try(OutputStream output = connection.getOutputStream()){ 
      output.write(query.getBytes(charset)); 
      //BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      //while((inputline=in.readLine()) != null){ 
      // Log.i("tago" , inputline); 
      //}in.close(); 
      //InputStream response = connection.getInputStream(); 
      Log.i("tago", "VeriTabani yazdım"); 
     }catch(IOException e){ 
      e.printStackTrace(); 
      Log.i("tago" , "VeriTabani yazamadım"); 
     } 
     try { 
      int status = connection.getResponseCode(); 
      BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream())); 
      String inputline; 
      while((inputline=in.readLine()) != null){ 
       Log.i("tago" , inputline); 
       JsondanCevir(inputline); 
      }in.close(); 
      Log.i("tago" , "VeriTabani status= " +status); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return "alabama"; 
    } 
+1

500意味着您所使用的服務器是一個那崩潰沒有Android應用 –

+0

根據我研究大多是Tomer,但我的服務器工作的朋友堅持認爲問題與android方面有關。 – Bad0

+0

你的*朋友*不是你的*朋友*。 Tomer說得對。雖然你可能已經構建了一個意想不到的要求,但是沒有關於要求的更多細節,Tomer是對的 – eduyayo

回答

0

這是沒有答案的,請嘗試使用此代碼,並分享結果,代碼沒有進行測試,但如果服務器的響應是正確的,這可能是作品,否則,驗證您的服務器。

PD:在試驗使用本明碼,任何附加的屬性或則將DoInput /輸出不需要

package com.test.test; 

import java.io.BufferedInputStream; 
import java.io.IOException; 
import java.net.HttpURLConnection; 
import java.net.URL; 

class receiveResponse { 
    String Response(String name, String urrl, String longi, String lat) 
      throws IOException { 
       String url = "http://185.22.184.103/project/connection.php?name="+URLEncoder.encode(name, "ISO-8859-9")+"&url="+urrl+"&long="+longi+"&lat="+lat; 
     URL link = new URL(url); 
     HttpURLConnection conn = (HttpURLConnection) link.openConnection(); 
     conn.setConnectTimeout(10000); 
     conn.setReadTimeout(10000); 

     BufferedInputStream is = new BufferedInputStream(conn.getInputStream()); 

     int ch; 
     StringBuilder sb = new StringBuilder(); 
     while ((ch = is.read()) != -1) { 
      sb.append((char) ch); 
     } 
     is.close(); 
     return sb.toString(); 
    } 
}