Mad*_*Alb 0 java url search android android-edittext
所以每次我运行应用程序时,输入一个ID并点击提交; 最终结果以null作为ID.
例如:我想输入12345它应该去http://www.hiddenlink.com/12345,但它转到http://www.hiddenlink.com/null
这是我的代码:
public class SearchActivity extends Activity {
Button btnSearchStudent;
String studentID;
private String url = "http://www.hiddenlink.com/" + studentID; // This is test SID just to confirm connection
private static final String TAG_ALLRECORDS = "Objects";
private static final String TAG_ENAME = "emer_name1";
private static final String TAG_EPHONE1 = "emer_Phone1";
private static final String TAG_EPHONE2 = "emer_Phone2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
EditText txtStudentID = (EditText) findViewById(R.id.txtStudentID);
studentID = txtStudentID.getText().toString();
btnSearchStudent = (Button) findViewById(R.id.btnSearchSID);
btnSearchStudent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SearchActivity.this, SearchResults.class);
startActivity(intent);
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<Void, Void, Void> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SearchActivity.this);
pDialog.setMessage("Connection Test: Logging into Student Database...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
// Login to APIs
@Override
protected Void doInBackground(Void... args) {
String _username = "hidden";
String _password = "hidden123";
String content = MyHttpURLConnection.getData(url, _username, _password);
try {
// Getting JSON Object from URL Content
JSONObject json = new JSONObject(content);
JSONArray jsonArray = json.getJSONArray(TAG_ALLRECORDS);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
// Storing JSON item in a Variable
String emer_name1 = c.getString(TAG_ENAME);
String emer_Phone1 = c.getString(TAG_EPHONE1);
String emer_Phone2 = c.getString(TAG_EPHONE2);
// Adding value HashMap key => value
HashMap<String, String> add = new HashMap<String, String>();
add.put(TAG_EPHONE1, emer_Phone1);
add.put(TAG_ENAME, emer_name1);
add.put(TAG_EPHONE2, emer_Phone2);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
String studentID;
private String url = "http://www.hiddenlink.com/" + studentID;
Run Code Online (Sandbox Code Playgroud)
studentID尚未在此处初始化,因此其值为null.删除+ studentID零件并在studentID设置值时添加.