2014-01-20 106 views
0

我已经为我的swing应用程序写入了一个代码来登录,我应该从web服务获取标志作为json格式的响应,我遇到的麻烦是当我发布数据时,如果我设置了某些标题或者500内部服务器错误(如果标题未设置),则会变糟400请求。错误的请求错误

连接类

public class Connection extends Thread { 

private String url1; 
private JSONObject data; 
String line; 
//Constuctor to initialize the variables. 

public Connection(String url1, JSONObject data) { 
    this.url1 = url1; 
    this.data = data; 
    start(); 
} 

public void run() { 
    ConnectionReaderWriter(); 
} 

//To fetch the data from the input stream 
public String getResult() { 
    return line; 
} 

public String ConnectionReaderWriter() { 
    URL url; 
    HttpURLConnection connection = null; 
    ObjectOutputStream out; 
    try { 
     /*URL url = new URL(Url.server_url + url1);  //Creating the URL.  
     URLConnection conn = url.openConnection(); //Opening the connection. 
     conn.setDoOutput(true); 
     OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
     wr.write(data); //Posting the data to the ouput stream. 
     wr.flush(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     line=rd.readLine();  //Reading the data from the input stream.  
     wr.close(); 
     rd.close();*/    
     url = new URL(Url.server_url + url1);  //Creating the URL. 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("POST"); 
     // Headers 
     connection.setRequestProperty("Content-Type", "application/json"); 
     connection.setRequestProperty("Accept", "application/json"); 
     // API key has to be passed 
     connection.setRequestProperty("api_key", "123456"); 
     connection.setUseCaches(false); 
     //System.out.println(connection.getRequestProperty("api_key"));    
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     //Send request 
     out = new ObjectOutputStream(connection.getOutputStream()); 
     out.write(data.toString().getBytes()); 
     out.flush(); 
     System.out.println(data.toString()); 
     int rescode = connection.getResponseCode(); 
     line = connection.getResponseMessage(); 
     System.out.println(line +" : "+ rescode); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));    
     line = rd.readLine();  //Reading the data from the input stream.  
     rd.close(); 
     out.close();    
     System.out.println("fsgjv: "); 

    } catch (MalformedURLException ex) { 
     Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex); 
     String nonet = "No Network Connection"; 
     line = nonet; 
    } catch (IOException ex) { 
     Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex); 
     String nonet = "No Server Connection"; 
     line = nonet; 
    } 
    return line; //Return te stream recived from the input stream. 
} 
} 

生成下面给出该错误。

{"username":"[email protected]","password":"123"} 
Bad Request : 400 
Jan 20, 2014 6:00:04 PM SupportingClass.Connection ConnectionReaderWriter 
SEVERE: null 
java.io.IOException: Server returned HTTP response code: 400 for URL:  http://192.168.10.241/MobpazAdmin/web/app_dev.php/ws/terminalusers/terminaluserlogins.json 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:525) 
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674) 
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1672) 
at java.security.AccessController.doPrivileged(Native Method) 
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1670) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243) 
at SupportingClass.Connection.ConnectionReaderWriter(Connection.java:81) 
at SupportingClass.Connection.run(Connection.java:40) 
    Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://192.168.10.241/MobpazAdmin/web/app_dev.php/ws/terminalusers/terminaluserlogins.json 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625) 
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468) 
at SupportingClass.Connection.ConnectionReaderWriter(Connection.java:78) 
... 1 more 

请告诉我,如果我在代码中犯了任何错误,我已经检查了它正在正常工作的服务器端代码。那就是要传递的数据是JSON对象下面

{"username":"[email protected]","password":"123"} 

回答

0

我改变这种访问方法现在我使用Httpget来实现相同。您必须下载相应的httpcomponents-client jar文件并将其添加到库中。使用的代码如下所示

data = URLEncoder.encode("searchvariable", "UTF-8") + "=" + URLEncoder.encode("name", "UTF-8"); 
HttpGet g = new HttpGet(Url.server_url + url1+"?"+data); 

数据是要与URL一起传递的参数。这个问题是由于访问方式错误造成的。

0

给出的问题,因为它返回一个状态代码400,您可以检查服务器日志,以找到更多的信息从服务器端传来的?