-2

如何将 ArrayList<> 转换为字符串 ...

细节:

step1:发送电子邮件地址到 php mySql 数据库 step2:如果电子邮件地址与数据库内容匹配,则回显成功 step3:检索 php 回显值(“success”) step4:将响应分配给字符串 phpEchoedVal step5:比较 phpEchoedVal 与“成功” step6:将电子邮件写入 sd 卡

这个可以吗//url = 电子邮件地址

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 {



            ...
        }
   }
4

2 回答 2

2
ArrayList<MyType> l = new ArrayList<MyType>();
// fill

System.out.println(l.toString());
于 2012-05-24T11:02:49.313 回答
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,entity 是一个 HttpEntity,其余的应该是不言自明的。

于 2012-05-24T11:37:50.920 回答