English 中文(简体)
轉換矩陣列清單@ Name ValuePair @ 轉換成字符串與機器人 java
原标题:Convert ArrayList<<NameValuePair>> to string android java
  • 时间:2012-05-24 11:01:18
  •  标签:
  • java
  • android

如何转换矩阵List< & gt; 到字符串...

细节 :

step1: send e-mail address to php mySql database step2: if email address match database content, echoed success step3: retrieved the php echoed value ("success") step4: assign the the response to a string phpEchoedVal step5: compare phpEchoedVal with "success" step6: write email to sd card

is this one ok //url = email address

阵列名ValuePair = 新的阵列名(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 {



            ...
        }
   }
最佳回答

对于您的第一个问题; 如何将数组列表中的字符串( 包含名称对数对象) 与其他字符串匹配 :

    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() returns an HttpResponse. From HttpResponse you can call getEntity which returns an HttpEntity From HttpEntity you can call getContent which returns an 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());
        }   

Note that IOUtility is self written class in this case. I think there are other ways to convert a HttpResponse to a string, but this is how I do it.

_Stream is importStream, 实体是 Httpentity, 其余的应该是自我解释。

问题回答
ArrayList<MyType> l = new ArrayList<MyType>();
// fill

System.out.println(l.toString());




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签