Android登录未经授权
我从事连接到web服务的android项目。有一个功能显示用户登录后的数据。登录函数使用http post方法调用web服务。以下是我连接到服务器的登录代码:Android登录未经授权,android,http,authentication,login,Android,Http,Authentication,Login,我从事连接到web服务的android项目。有一个功能显示用户登录后的数据。登录函数使用http post方法调用web服务。以下是我连接到服务器的登录代码: public HttpResponse makeRequestNew(String path, String params, String params2) throws ClientProtocolException, IOException { httpclient = new DefaultHttpClient(
public HttpResponse makeRequestNew(String path, String params, String params2) throws ClientProtocolException, IOException
{
httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(path);
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
List<NameValuePair> entity = new ArrayList<NameValuePair>();
entity.add(new BasicNameValuePair("name", params));
entity.add(new BasicNameValuePair("pass", params2));
try {
httppost.setEntity(new UrlEncodedFormEntity(entity));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Execute HTTP Post Request
/*
try {
return httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
*/
return httpclient.execute(httppost);
}
它返回未经授权的http 401
我的问题是为什么我的应用程序不能连接到服务器,即使我已经登录??如何解决这个问题??
感谢您使用
返回空值的原因代码>?好的,我会更改它。您使用的web服务是在哪个框架中开发的?我没有使用任何框架
public JSONObject getJSONFromUrlAuth(String url) {
// Making HTTP request
try {
// defaultHttpClient
Config c = new Config();
JSONObject jo = c.getSetting();
DefaultHttpClient httpClient = new DefaultHttpClient();
//CredentialsProvider credProvider = new BasicCredentialsProvider();
try {
//credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(jo.getString("username"), jo.getString("password")));
//httpClient.setCredentialsProvider(credProvider);
httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials(jo.getString("username"), jo.getString("password")));
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}