错误请求错误

时间:2014-01-20 12:43:41

标签: java json swing httpurlconnection

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

连接类

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":"ann.jake@gmail.com","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":"ann.jake@gmail.com","password":"123"}  

2 个答案:

答案 0 :(得分:0)

问题必须来自服务器,因为它返回状态代码400.你能检查服务器日志以找到更多信息吗?

答案 1 :(得分: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一起传递的参数。问题是由于错误的访问方法而发生的。