2015-09-03 70 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(); 
    } 
}