如何转换ArrayList<>串起来......
详细说明:
step1:将电子邮件地址发送到php mySql数据库 step2:如果电子邮件地址与数据库内容匹配,则表示成功 step3:检索php回显值(“成功”) step4:将响应分配给字符串phpEchoedVal 第五步:将phpEchoedVal与“成功”进行比较 step6:给sd卡写电子邮件
这是一个好的 //网址=电子邮件地址
ArrayList nameValuePair = new ArrayList(1);
nameValuePair.add(new BasicNameValuePair("url", url));
try {
//open an httpclient connection
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(strLink);
httppost.setEntity(new UrlEncodedFormEntity(data));
//response retrieve "success" echoed from server
ResponseHandler<String> handleResponse = new BasicResponseHandler();
//converts server response to string.
String phpEchoVal = httpclient.execute(httppost, handleResponse);
txtInfo.setText(phpEchoVal);
String containEmail = txtInfo.getText().toString();
//compare response from server with local string
// if successful write email to sd card
if ((containEmail.equals("success"))){
//create a new text file
File myFile = new File("/sdcard/moLarouteReg.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(containEmail);
//close writer
myOutWriter.close();
fOut.close();
//Open menu
Intent openMenu = new Intent("com.molaroute.mu.Menu");
startActivity(openMenu);
finish();
Log.d("test","fileWritten: "+myOutWriter);
}else {
...
}
}
答案 0 :(得分:2)
ArrayList<MyType> l = new ArrayList<MyType>();
// fill
System.out.println(l.toString());
答案 1 :(得分:2)
第一个问题;如何将数组列表中的字符串(包含名称值对象)与其他字符串匹配:
ArrayList<NameValuePair> lst = new ArrayList<NameValuePair>(); //Instantiate arraylist
lst.add(new BasicNameValuePair("fruit", "apple")); //add entry
lst.add(new BasicNameValuePair("animal", "monkey")); //add entry
String strToCompareAgainst = "apple"; //str to compare to for example
for(NameValuePair valPair : lst) { //foreach loop
if(valPair.getValue().equals(strToCompareAgainst)) { //retrieve value of the current NameValuePair and compare
Log.d("login","moooooo"); //success
}
}
您还可以检查文本是否包含其他文本,而不是检查是否相等。
String str1 = "I am a success";
String str2 = "success";
if(str1.contains(str2)) {} //Evaluates to true, because str1 contains the word "success"
if(str1.equals(str2)) {} //Evaluates to false, because str1 does not hold the same value as str2
当然,你可以做更多,但这应该给你基本的想法..
关于你的第二个问题:
HttpClient.execute()返回一个HttpResponse。 从HttpResponse你可以调用getEntity来返回一个HttpEntity 从HttpEntity可以调用getContent,它返回一个InputStream。
您可以将此InputStream转换为字符串。网上广泛使用转换代码段示例。
所以代码:
httpResponse = client.execute(post, _httpContext);
//Set the response code from the request's responst.
setResponseCode(httpResponse.getStatusLine().getStatusCode());
//Retrieve the body of the response.
entity = httpResponse.getEntity();
if(entity != null) {
//Retrieve the content from the body.
_inStream = entity.getContent();
//Convert the InputStream to String and set the String response to the returned value.
setStringResponse(IOUtility.convertStreamToString(_inStream));
//Close the InputStream.
Log.d(TAG, getStringResponse());
}
请注意,在这种情况下,IOUtility是自写类。 我认为还有其他方法可以将HttpResponse转换为字符串,但这就是我的方法。
_inStream是一个InputStream,实体是一个HttpEntity,其余部分应该是自我解释的。