HttpUrlConnection Klasse

  • 5 Antworten
  • Letztes Antwortdatum
D

Donni1234

Neues Mitglied
0
Hallo,

ich benötige Hilfe für die Verbindung und Login auf einer Website. Um genau zu sein auf der Website WebUntis.
Ich habe bereits eine API gefunden die Prima über meinen PC läuft. jedoch habe ich versucht das ganze für eine App zu schreiben und die Klasse wirft plötzlich Exceptions.
Code:
package at.toaster.client.data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

public class WebUntisRequests {

    protected static String getSessionID(URL url,
            String username, String password) throws JSONException, IOException {

        JSONObject json = new JSONObject();
        JSONObject params = new JSONObject();

        params.put("user", username);
        params.put("password", password);

        json.put("jsonrpc", "2.0");
        json.put("id", "1");
        json.put("method", "authenticate");
        json.put("params", params);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Content-Length",
                String.valueOf(json.length()));

        OutputStreamWriter writer = new OutputStreamWriter(
                connection.getOutputStream());

        writer.write(json.toString());
        writer.flush();   //diese Methode wirft Exception

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream())); //InputStream wirft Exception

        String output = "";
        String line = "";
        while ((line = reader.readLine()) != null) {
            output += line;
        }
       

        writer.close();
        reader.close();

        JSONObject jsonobject = new JSONObject(output);
       
        JSONObject result = new JSONObject(jsonobject.getString("result"));

        return result.getString("sessionId");

    }

    protected static JSONObject getData(URL url, String sessionID,
            JSONObject json) throws JSONException, IOException {

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Content-Length",
                String.valueOf(json.length()));
        connection.setRequestProperty("Cookie", "JSESSIONID=" + sessionID);

        OutputStreamWriter writer = new OutputStreamWriter(
                connection.getOutputStream());

        writer.write(json.toString());
        writer.flush();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));

        String output = "";
        String line = "";
        while ((line = reader.readLine()) != null) {
            output += line;
        }

        writer.close();
        reader.close();

        return new JSONObject(output);

    }

}

Hoffe mir kann jemand helfen.

MFG Donni 1234
 
Das wird sicher leichter, wenn du uns auch noch verrätst, welche (IO)Exception genau geworfen wird.
 
So, ich hab mal die LogCat rausgesucht für die beiden Methoden.

Hier die LogCat bei der flush Methode

06-30 12:25:39.875: W/System.err(3521): java.io.IOException: exceeded content-length limit of 4 bytes

06-30 12:25:39.890: W/System.err(3521): at org.apache.harmony.luni.internal.net.www.protocol.http.RetryableOutputStream.write(RetryableOutputStream.java:58)

06-30 12:25:39.898: W/System.err(3521): at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:164)

06-30 12:25:39.898: W/System.err(3521): at at.toaster.client.data.WebUntisRequests.getSessionID(WebUntisRequests.java:47)

06-30 12:25:39.906: W/System.err(3521): at at.toaster.client.data.WebUntisConnector.<init>(WebUntisConnector.java:44)

06-30 12:25:39.914: W/System.err(3521): at at.toaster.client.Client.<init>(Client.java:19)

06-30 12:25:39.921: W/System.err(3521): at alarmmanager.UntisActivity.buttonLog(UntisActivity.java:32)

06-30 12:25:39.929: W/System.err(3521): at java.lang.reflect.Method.invokeNative(Native Method)

06-30 12:25:39.937: W/System.err(3521): at java.lang.reflect.Method.invoke(Method.java:507)

06-30 12:25:39.945: W/System.err(3521): at android.view.View$1.onClick(View.java:2139)

06-30 12:25:39.945: W/System.err(3521): at android.view.View.performClick(View.java:2485)

06-30 12:25:39.960: W/System.err(3521): at android.view.View$PerformClick.run(View.java:9080)

06-30 12:25:39.968: W/System.err(3521): at android.os.Handler.handleCallback(Handler.java:587)

06-30 12:25:39.968: W/System.err(3521): at android.os.Handler.dispatchMessage(Handler.java:92)

06-30 12:25:39.976: W/System.err(3521): at android.os.Looper.loop(Looper.java:130)

06-30 12:25:39.984: W/System.err(3521): at android.app.ActivityThread.main(ActivityThread.java:3687)

06-30 12:25:39.992: W/System.err(3521): at java.lang.reflect.Method.invokeNative(Native Method)

06-30 12:25:40.000: W/System.err(3521): at java.lang.reflect.Method.invoke(Method.java:507)

06-30 12:25:40.007: W/System.err(3521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)

06-30 12:25:40.007: W/System.err(3521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)

06-30 12:25:40.015: W/System.err(3521): at dalvik.system.NativeStart.main(Native Method)


Hier die LogCat bei der InputStream Klasse
06-30 12:28:12.257: W/System.err(3697): java.io.IOException: content-length promised 4 bytes, but received 0

06-30 12:28:12.296: W/System.err(3697): at org.apache.harmony.luni.internal.net.www.protocol.http.RetryableOutputStream.close(RetryableOutputStream.java:48)

06-30 12:28:12.304: W/System.err(3697): at org.apache.harmony.luni.internal.net.www.protocol.http.RetryableOutputStream.contentLength(RetryableOutputStream.java:64)

06-30 12:28:12.312: W/System.err(3697): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.prepareRequestHeaders(HttpURLConnectionImpl.java:864)

06-30 12:28:12.320: W/System.err(3697): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.writeRequestHeaders(HttpURLConnectionImpl.java:787)

06-30 12:28:12.328: W/System.err(3697): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1028)

06-30 12:28:12.335: W/System.err(3697): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)

06-30 12:28:12.343: W/System.err(3697): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:258)

06-30 12:28:12.351: W/System.err(3697): at at.toaster.client.data.WebUntisRequests.getSessionID(WebUntisRequests.java:49)

06-30 12:28:12.359: W/System.err(3697): at at.toaster.client.data.WebUntisConnector.<init>(WebUntisConnector.java:44)

06-30 12:28:12.367: W/System.err(3697): at at.toaster.client.Client.<init>(Client.java:19)

06-30 12:28:12.367: W/System.err(3697): at alarmmanager.UntisActivity.buttonLog(UntisActivity.java:32)

06-30 12:28:12.367: W/System.err(3697): at java.lang.reflect.Method.invokeNative(Native Method)

06-30 12:28:12.367: W/System.err(3697): at java.lang.reflect.Method.invoke(Method.java:507)

06-30 12:28:12.375: W/System.err(3697): at android.view.View$1.onClick(View.java:2139)

06-30 12:28:12.375: W/System.err(3697): at android.view.View.performClick(View.java:2485)

06-30 12:28:12.375: W/System.err(3697): at android.view.View$PerformClick.run(View.java:9080)

06-30 12:28:12.382: W/System.err(3697): at android.os.Handler.handleCallback(Handler.java:587)

06-30 12:28:12.382: W/System.err(3697): at android.os.Handler.dispatchMessage(Handler.java:92)

06-30 12:28:12.382: W/System.err(3697): at android.os.Looper.loop(Looper.java:130)

06-30 12:28:12.390: W/System.err(3697): at android.app.ActivityThread.main(ActivityThread.java:3687)

06-30 12:28:12.390: W/System.err(3697): at java.lang.reflect.Method.invokeNative(Native Method)

06-30 12:28:12.390: W/System.err(3697): at java.lang.reflect.Method.invoke(Method.java:507)

06-30 12:28:12.398: W/System.err(3697): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)

06-30 12:28:12.398: W/System.err(3697): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)

06-30 12:28:12.398: W/System.err(3697): at dalvik.system.NativeStart.main(Native Method)

Hoffe das hilft euch mein Problem zu lösen ;)

MFG Donni1234
 
So aus der Exception und deinem Code heraus würde ich sagen, json.length() ist nicht die Länge die du suchst. Du willst da ja auch die Länge des JSON-Strings in Byte und nicht die Anzahl der Schlüssel-Wert-Paarungen.
 
Ja das war es. Vielen Dank für die Hilfe.
 
Zurück
Oben Unten