我是android新手。在这个应用程序中,我创建了一个登录页面,用户只能登录,如果该用户名和密码与URL中的用户名和密码匹配,但作为响应,它显示空字符串,并捕获超时错误的异常。这是代码。
public class MainActivity extends AppCompatActivity {
Button button;
String connectionString, params;
PackageManager pm;
SharedPreferences preferences;
DatabaseHelper mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText username = (EditText) findViewById(R.id.editText);
final EditText password = (EditText) findViewById(R.id.editText2);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.button1) {
String userNameString = username.getText().toString();
String passwordString = password.getText().toString();
String url = "http://122.160.78.189:85/mobile/loginUser?";
String params = null;
try {
params = "userName=" + URLEncoder.encode(userNameString, "UTF-8") + "&password=" + URLEncoder.encode(passwordString, "UTF-8") + "&appVersion= 1.22";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
new Mytask().execute(url, params);
}
}
}));
}
class Mytask extends AsyncTask<String, Void, String> {
// Runs in UI before background thread is called
@Override
protected void onPreExecute() {
setProgressBarVisibility(true);
// Do something like display a progress bar
}
// This is run in a background thread
@Override
protected String doInBackground(String... params) {
return getFromServer(params[0], params[1]);
}
// This runs in UI when background thread finishes
@Override
protected void onPostExecute(String result) {
try {
checkLogin(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
public String getFromServer(String connectionString, String params) {
String response = "";
try {
// android.os.Debug.waitForDebugger();
URL url = new URL(connectionString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//con.setConnectTimeout(100 * 1000); //10 Seconds
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length", "" + Integer.toString(params.getBytes().length));
con.setRequestProperty("Content-Language", "en-US");
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(params);
wr.flush();
wr.close();
InputStream is = con.getInputStream();
response = read(is);
} catch (Exception e) {
e.printStackTrace();
} finally {
return response;
}
}
private String read(InputStream in) {
BufferedReader reader;
StringBuilder response = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return response.toString();
}
}
}
}
答案 0 :(得分:0)
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new
URL(url).openConnection();
con.setRequestMethod("HEAD");
con.setConnectTimeout(5000); //set timeout to 5 seconds
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (java.net.SocketTimeoutException e) {
return false;
} catch (java.io.IOException e) {
return false;
}
答案 1 :(得分:0)
您可以增加连接并读取超时时间。
con.setConnectTimeout(connectiontimeout);
con.setReadTimeout(readtimeout);
您可以使用[改装] http://square.github.io/retrofit/
对于您当前的代码,此服务器响应是, {&#34;用户id&#34;:0,&#34;用户名&#34;:空,&#34;名称&#34;:空,&#34;密码&#34;:空,&#34;作用& #34;:0,&#34;电子邮件&#34;:空,&#34;移动&#34;:空,&#34;部门&#34;:空,&#34; selectedState&#34;:空, &#34; selectedmainGroup&#34;:空}