HTML response from server

后端 未结 8 1632
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 11:21

I have an app that submits some from data to a local server and as a result the server will send back a JSON. say {status:\"success\"}

Its working when I run the ap

8条回答
  •  [愿得一人]
    2021-01-13 11:32

    alright rahul have you tried any different kind of response handling for the HTTP Post protocol

    i use some thing like this.

    try {
                     String params;
                     String encodedDataLength;
                    connectURL = new URL(_uRL); 
    
                     params = "Blah... Blah... blah;
    
                     encodedDataLength = "" + params.length();
    
    
                     conn =  (HttpURLConnection) connectURL.openConnection();
    
    
                     //conn.setDoInput(true);
                    conn.setDoOutput(true); // signify a post communcation protocol
    
    
    
                    conn.setRequestProperty("User-Agent","mobile");
                    conn.setRequestProperty("Content-Language", "en-US");
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    conn.setRequestProperty( "Content-Length", encodedDataLength);
    
                    //System.out.println("con :" + conn);
                    os = conn.getOutputStream();
                    os.write(params.getBytes());
    
                    os.flush();
    
                    // Getting the response code will open the connection,
                    // send the request, and read the HTTP response headers.
                    // The headers are stored until requested.
    
    
                    is = conn.getInputStream();
    
                    // get session from the cookie
                    String cookie = conn.getHeaderField("cookie");
                    if (cookie != null) {
                        session = cookie;
    
                    }
    
    
                    // Get the length and process the data
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    responseString = br.readLine();
    
                    //System.out.println(responseString);
    
    
    
                } catch (Exception e) {
                    //java.lang.System.out.println("http exception: " + e.printStackTrace());
    
    
    
    
    
    
                } finally {
                    try {
                        if (is != null)
                            is.close();
                        if (os != null)
                            os.close();
                        if (conn != null)
                            conn.disconnect();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
    

    after this is done... you can just insert any Json decoding methodology to retrive the data from the string called resposeString.

    Below are the included import files for this implementation. I know there are different for those.

    import java.io.*;
    import java.net.URL;
    import java.net.HttpURLConnection;
    import java.util.zip.GZIPInputStream;
    import java.lang.String;
    

提交回复
热议问题