Soru & Cevap

.Net Web Api - Android Post İşlemi ...

16.07.2017 - 02:38

Merhabalar;

Bir uygulama yazıyorum ve uygulama için uzak MySql e data kaydetmem gerek.

.Net Web Api kullanarak servisimi yazdım ve browserlardan test ettiğimden düzgün çalışmakta.

Android uygulamasından çalıştırdığımda ise Object reference not set to an instance of an object. hatası alıyorum. Yani Json boş gidiyor servise.

Bu konuda yardımcı olursanız memnun olurum.

 

Android Kodu ....

String jsonData=params[0];
        try {
            URL url = new URL("http://webservice.brainboxsoft.com/api/Vehicle/addVehicleController");
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            try {


               httpURLConnection.setDoInput (true);
               httpURLConnection.setDoOutput (true);
               // httpURLConnection.setUseCaches (false);

                httpURLConnection.connect();
            } catch (Exception e) {
                Log.d("Web Api",e.toString());
            }

            //Write
            OutputStream dos =(OutputStream) httpURLConnection.getOutputStream();
            dos.write(jsonData.getBytes());
            InputStream is =httpURLConnection.getInputStream();
            String result = null;
            int byteCharacter;
            StringBuilder sb = new StringBuilder();

            while ((byteCharacter = is.read()) != -1) {
                result+=(char)byteCharacter;
            }

            is.close();
            dos.close();
            Log.d("Web Api","RETURN : "+result);

            httpURLConnection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

 

4 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user gmyucalfarukeren
gmyucalfarukeren
22.02.2023 - 01:20

Merhaba Levent Bey,

"Object reference not set to an instance of an object" hatası genellikle bir nesne referansına null bir değer atanması veya bir nesneye erişmeye çalışırken null bir referansa sahip olunması durumunda oluşur. Bu durumda, kodunuzu incelemeniz ve hangi nesnenin null olduğunu belirlemeniz gerekiyor

Bununla birlikte, görünüşe göre, "jsonData" değişkeninizin null olduğunu veya boş bir dize olduğunu düşünüyorum. Bu nedenle, httpURLConnection.getOutputStream() yöntemini çağırmaya çalıştığınızda hata oluşur. Bu yöntem, istek gövdesinin yazılacağı OutputStream nesnesi döndürür ve istek gövdesi verileri bu nesne aracılığıyla yazılır

Kodunuzu düzeltmek için, json verilerinin istek gövdesine doğru şekilde yazıldığından emin olun. Ayrıca, "jsonData" değişkeninin null olmadığından ve doğru verileri içerdiğinden emin olmak için "Log.d" yöntemini kullanarak değişken içeriğini loglamanızı öneririm

Aşağıdaki gibi kodu değiştirmeyi deneyebilirsiniz

 

try {
    String jsonData = params[0];
    URL url = new URL("http://webservice.brainboxsoft.com/api/Vehicle/addVehicleController");
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setRequestProperty("Content-Type", "application/json");

    // http ıvırzıvırları bilmem ne 
    httpURLConnection.setDoInput(true);
    httpURLConnection.setDoOutput(true);
    OutputStream outputStream = httpURLConnection.getOutputStream();
    outputStream.write(jsonData.getBytes("UTF-8"));
    outputStream.flush();
    outputStream.close();

    int responseCode = httpURLConnection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuilder responseBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            responseBuilder.append(line);
        }
        String response = responseBuilder.toString();
        Log.d("Web Api", "Response: " + response);
        inputStream.close();
    } else {
        Log.d("Web Api", "Response code: " + responseCode);
    }
} catch (Exception e) {
    Log.d("Web Api", "Exception: " + e.toString());
}