6

मैं एक रेल अनुप्रयोग सर्वर से कनेक्ट करने का प्रयास कर रहा हूं जिसके लिए प्रमाणीकरण की आवश्यकता है। मैं डेस्कटॉप अनुप्रयोग पर जावा के लिए जकार्ता HTTP क्लाइंट का उपयोग कर रहा हूं और यह 100% काम करता है। लेकिन जब एंड्रॉइड एमुलेटर पर सटीक एक ही कोड निष्पादित किया जाता है तो मुझे एक IOException मिलता है।एंड्रॉइड फोन पर रेल सर्वर सर्वर पर मूल HTTP प्रमाणीकरण

यहां कोड है, और यदि कोई मुझे यह समझने में मदद कर सकता है कि यह आईओएक्सप्शन को क्यों फेंकता है जिसकी सराहना की जाएगी!

private boolean login() 
{ 
    String username, password; 

    DefaultHttpClient client; 
    AuthScope scope; 
    Credentials myCredentials; 
    CredentialsProvider provider; 
    HttpEntity entity; 
    String line; 
    BufferedReader reader; 
    InputStream instream; 

    //Declare & Create the HTTP Client 
    client = new DefaultHttpClient(); 

    //Create our AuthScope 
    scope = new AuthScope("10.19.9.33", 3000); 

    username = "admin" 
      password = "pass" 


    //Set Credentials 
    myCredentials = new UsernamePasswordCredentials(username, password); 

    //Set Provider 
    provider = new BasicCredentialsProvider(); 
    provider.setCredentials(scope, myCredentials); 

    //Set Credentials 
    client.setCredentialsProvider(provider); 

    String url = "http://10.19.9.33:3000/users/show/2"; 

    HttpGet get; 

    //Tell where to get 
    get = new HttpGet(url); 

    HttpResponse response; 

    try 
    { 
     response = client.execute(get); 

     entity = response.getEntity(); 

     /* Check to see if it exists */ 
     if(entity != null) 
     { 
      instream = entity.getContent(); 

      try { 

       reader = new BufferedReader(new InputStreamReader(instream)); 

       line = reader.readLine(); 

       if(line.equals("HTTP Basic: Access denied.")) 
        return false; 

       while (line != null) 
       { 
        // do something useful with the response 
        System.out.println(line); 

        line = reader.readLine(); 
       } 

       return true; 

      } 
      catch (IOException ex) 
      { 

       // In case of an IOException the connection will be released 
       // back to the connection manager automatically 
       throw ex; 

      } 
      catch (RuntimeException ex) 
      { 
       // In case of an unexpected exception you may want to abort 
       // the HTTP request in order to shut down the underlying 
       // connection and release it back to the connection manager. 
       get.abort(); 
       throw ex;    
      } 
      finally 
      { 
       // Closing the input stream will trigger connection release 
       instream.close();    
      } 
     } 
    } 
    catch(ClientProtocolException cp_ex) 
    { 

    } 
    catch(IOException io_ex) 
    { 

    } 

    return false; 
} 
+3

आप टी के प्रासंगिक अंश प्रदान कर सके वह ट्रेस ढेर? –

+0

इनपुटस्ट्रीम के लिए एपीआई संदर्भ कहता है: यह अमूर्त वर्ग पूरी तरह से कार्यान्वित कार्यान्वयन प्रदान नहीं करता है, इसलिए इसे उप-वर्गीकृत करने की आवश्यकता है, और कम से कम पढ़ने() विधि को ओवरराइड करने की आवश्यकता है। BufferedInputStream आज़माएं। – techiServices

+0

मुझे पता चला कि यह पता से कनेक्ट नहीं होने का कारण यह था कि मैं मैनिफेस्ट में इंटरनेट अनुमति जोड़ना भूल गया था। लेकिन, अब डिवाइस "प्रतिक्रिया = client.execute (प्राप्त) पर लटका हुआ प्रतीत होता है;" लाइन ... –

उत्तर

2

कारण यह ट्रिगर IOException था, क्योंकि मैनिफ़ेस्ट फ़ाइल मैं काम के इस प्रकार के लिए HttpPost उपयोग कर रहा हूँ इंटरनेट

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
0

के लिए आवेदन अधिकार नहीं दिया रखा, और नहीं था किसी भी समस्या:

[...] 
DefaultHttpClient client = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(LOGIN_SERVLET_URI); 
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); 
params.add(new BasicNameValuePair("userName", userName)); 
params.add(new BasicNameValuePair("password", password)); 

UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); 
httppost.setEntity(p_entity); 
HttpResponse response = client.execute(httppost); 
HttpEntity responseEntity = response.getEntity(); 
[...] 

शायद यह तुम बाहर में मदद करता है

संबंधित मुद्दे