简体   繁体   中英

Android to server 500 error

I am trying to send user informations to server via open connection and it returns user id in JSON. Everything is OK but when i am trying to read user id with getInputStream i can't read it (i take 500 server error). When i try it with getErrorStream and log the result the JSON is come here. Why ? How can i overcome it ?

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";
    }

This is not the answer, try using this code and share the result, the code is not tested but if the server response is correct this could be works, else, verify your server.

PD: In test use this plain code, any additional property or doInput/output is not required

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM