سؤال

أنا مشكلة في httpsurlconnection لا يمكنني حلها. في الأساس ، أقوم بإرسال بعض المعلومات إلى خادم وإذا كانت بعض هذه البيانات خاطئة ، فإن الخادم يرسل لي رمز استجابة 500. ومع ذلك ، فإنه يرسل أيضًا رسالة في الرد تخبرني بأي جزء من البيانات كان خطأ. تكمن المشكلة في أن الرسالة فارغة دائمًا عندما أقرأها. أعتقد أن هذا لأن استثناء FileNotfound يتم طرحه دائمًا قبل قراءة الدفق. هل انا على حق؟ حاولت قراءة ErrorStream أيضًا ولكن هذا دائمًا فارغ. هذا مقتطف:

    conn = (HttpsURLConnection) connectURL.openConnection();
    conn.setDoOutput(true);
    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Length",
         Integer.toString(outString.getBytes().length));
    DataOutputStream wr = new DataOutputStream(conn
      .getOutputStream());
    wr.write(outString.getBytes());
    wr.flush();
    wr.close();
    if(conn.getResponseCode>400{

    String response = getErrorResponse(conn);

    public String getErrorResponse(HttpsURLConnection conn) {
    Log.i(TAG, "in getResponse");
    InputStream is = null;
    try {

     //is = conn.getInputStream();
    is = conn.getErrorStream();
    // scoop up the reply from the server
    int ch;
    StringBuffer sb = new StringBuffer();
    while ((ch = is.read()) != -1) {
     sb.append((char) ch);
    }
    //System.out.println(sb.toString());
    return sb.toString();
    // return conferenceId;
   }
    catch (Exception e){
    e.printStackTrace();
    }
    }
هل كانت مفيدة؟

المحلول

لذلك فقط لمتابعة هذا ، إليك كيف قمت بحلها:

public static String getResponse(HttpsURLConnection conn) {
    Log.i(TAG, "in getResponse");
    InputStream is = null;
    try {
        if(conn.getResponseCode()>=400){
            is = conn.getErrorStream();
        }
        else{
            is=conn.getInputStream();
        }
        ...read stream...
}

يبدو أن وصفهم مثل هذا أنتج دفق خطأ مع رسالة. شكرا للاقتراحات!

نصائح أخرى

حاول تعيين خاصية طلب نوع المحتوى إلى "application/x-www-form-urlencoded"

تم ذكر الشيء نفسه على هذا الرابط:http://developers.sun.com/mobility/midp/ttips/httppost/

The Content-Length and Content-Type headers are critical because they tell the web server how many bytes of data to expect, and what kind, identified by a MIME type.

In MIDP clients the two most popular MIME types are application/octet-stream, to send raw binary data, and application/x-www-form-urlencoded, to send name-value pairs

هل أنت تتحكم في الخادم؟ بمعنى آخر ، هل كتبت العملية التي تعمل على الخادم وتستمع إلى المنفذ الذي تحاول الوصول إليه؟

إذا فعلت ذلك ، فيجب أن تكون قادرًا أيضًا على تصحيحه ومعرفة سبب إرجاع عمليتك 404.

إذا لم تقم بذلك ، فقم بصف الهندسة المعمارية (HTTP Server ، والمكون الذي يستدعيه للرد على طلب HTTP (S) ، وما إلى ذلك) وسنأخذه من هناك.

في أبسط حالة ، من خادم HTTP كونه خادم Apache الذي يعطي عنصر تحكم لبعض البرنامج النصي PHP ، فهذا يعني أن Apache لم يتمكن من تعيين طلبك إلى أي شيء. على الأرجح خادم خادم ويب. قدم المزيد من التفاصيل وسنساعدك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top