-1

我正在尝试为我的应用程序创建登录名。当我在 web 浏览器中尝试给定的 url 时,它工作正常,但 web 服务无法在 android 应用程序中使用以下代码。出于测试目的,我对 url 链接进行了硬编码。我已经在 manifest.xml 文件中设置了 Internet 权限。

String uri= "http://192.168.1.6:8080/Cloud/webresources/generic/login?user=ankita&pass=1234";
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(uri);
    HttpResponse response = httpclient.execute(httpget);
    StatusLine statusLine = response.getStatusLine();
    if(statusLine.getStatusCode() == HttpStatus.SC_OK)
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        out.close();
        String responseString = out.toString();
        if(responseString.equals("success"))
        {
            System.out.println("Successful login");
            Intent intent = new Intent(this, DisplayDetailsActivity.class);
            String message = "User id is: success";//+userid.toString();
            intent.putExtra(EXTRA_MESSAGE, message);
            startActivity(intent);
        }
        else
        {
            System.out.println("Unsuccessful login");
        }
    }

任何帮助将不胜感激。谢谢。

4

3 回答 3

0

你可以试试下面的代码

 try {
        DefaultHttpClient client = new DefaultHttpClient();

        String uri= "http://192.168.1.6:8080/Cloud/webresources/generic/login?"

        String request = uri + java.net.URLEncoder.encode("user=ankita&pass=1234", "UTF-8");

        HttpGet get = new HttpGet(request);
        HttpResponse responseGet = client.execute(get);
        HttpEntity resEntityGet = responseGet.getEntity();

        if (resEntityGet != null) {
            String response = EntityUtils.toString(resEntityGet);
            Log.i("Search Response", response);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

你有没有在清单中写下这两行

<uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
于 2013-06-05T08:49:20.213 回答
0

你可以试试这段代码。

 HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://192.168.1.6:8080/Cloud/webresources/generic/login");
    try {
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
      nameValuePairs.add(new BasicNameValuePair("user","ankita"));
       nameValuePairs.add(new BasicNameValuePair("pass","1234"));
      post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

      HttpResponse response = client.execute(post);
      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
      String line = "";
      while ((line = rd.readLine()) != null) {
        System.out.println(line);
      }

    } catch (IOException e) {
      e.printStackTrace();
    }

在AndroidManifest.xml文件中放置适当的权限。

于 2013-06-05T09:25:52.353 回答
0

@Ankita您是否在 AsyncTask Thread 中添加了代码?

于 2013-06-05T09:28:48.820 回答