开发者

Comparing result from the server with a string in java 2ME

开发者 https://www.devze.com 2023-03-17 10:10 出处:网络
I\'m working on a java 2ME app that submit data to mysql database (am running PHP as the scripting language), but am having problem to compare string with the result that comes from the server. I want

I'm working on a java 2ME app that submit data to mysql database (am running PHP as the scripting language), but am having problem to compare string with the result that comes from the server. I want to compare a string that has been print with PHP eg. echo "OK"; with a string String str = "OK" within my java 2ME codes. Can anyone help me with this? Here are my codes

register.java

public void register() {
        String result = null;
        PostHttp post = new  PostHttp();

        post.setDocument("birth");
        post.setPath("/notification/");

        //Set Parameters
        post.setParameter("firstname", getChildFirstName().getString());
        post.setParameter("middlename", getChildMiddleName().getString());
        post.setParameter("lastname", getChildLastName().getString());
        post.setParameter("gender", getChildGender().getString(getChildGender().getSelectedIndex()));/***************/
        post.setParameter("childDOB", getChildDOB().getDate().toString());/*******************/
        post.setParameter("birthType", getBirthType().getString(getBirthType().getSelectedIndex()));
        post.setParameter("weight", getBornWithWeight().getString());
        post.setParameter("mother_fname", getMotherFirstName().getString());
        post.setParameter("mother_mname", getMotherMiddleName().getString());
        post.setParameter("mother_lname", getMotherLastName().getString());
        post.setParameter("birthPlace", getPlaceOfBirth().getString());
        post.setParameter("residence", getResidence().getString());
        post.setParameter("residence", getResidence().getString());

        result = post.submit();
        String x = "OK";
        if(result.compareTo(x)){
           this.append("Equal Strings"); 
        }else{
       this.append("Not equal"); 
}
    }

PostHttp.java

import java.io.*;
import javax.microedition.io.*;

public class PostHttp {

    private String protocol;
    private String host;
    private String path; //path to the file
    private String document; // controller function
    private String data; // Post Data

    public PostHttp() {
        // Set the defaults
        protocol = "http://";
        host = "localhost/badis/index.php";
        path = "/notification/";
        document = "trial";
        data = "";

    }

    public void setProtocol(String p) {
        protocol = p;
    }

    public void setHost(String h) {
        host = h;
    }

    public void setPath(String p) {
        path = p;
    }

    public void setDocument(String d) {
        document = d;
    }

    public void setParameter(String parameter, String value) {

        if (data.length() > 0) {
            data = data + "&";
        }
        data = data + parameter + "=" + value;
    }

    HttpConnection setupConnection(String uri) throws IOException {
        HttpConnection connection = null;

        connection = (HttpConnection) Connector.open(uri, Connector.READ_WRITE);
        connection.setRequestMethod(HttpConnection.POST);
        connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Host", host);
        return (connection);
    }

    void sendPostData(HttpConnection connection, String postData) throws IOException {
        OutputStream outputStream = null;

        outputStream = connection.openOutputStream();
        outputStream.write(postData.getBytes());
        outputStream.close();
    }

    String readResult(HttpConnection connection) throws IOException {
        InputStreamReader inputStream;
        String result = "";
        final int arbitraryBufferSize = 100;
        final int endOfData = -1;
        char[] characterBuffer = new char[arbitraryBufferSize];
        int bytesRead;

        inputStream = new InputStreamReader(connection.openInputStream());
        bytesRead = inputStream.read(characterBuffer);
        while (bytesRead != endOfData) {
            result = result + new String(characterBuffer);
            bytesRead = inputStream.read(characterBuffer);
        }
        inputStream.close();
        return (result);
    }

    public String submit() {
        HttpConnection connection;
        String result;

        try {
            connection = setupConnection(protocol + host + path + document);
            sendPostData(connection, data);
            result = readResult(connection);
            connection.close();
        } catch (IOException ioe) {
            result = "IOException!";
        开发者_StackOverflow社区}
        return (result);
    }
}

Result

 Not equal

php script

 public function birth() {
     echo "OK";
}

am using codeigniter 2.0.1, if that helps.

Thanx in advance.. Cheers


Lets be absolutely clear about this. If the following is appending "Not equal":

    String x = "OK";
    if (result.equals(x)) {
       this.append("Equal Strings"); 
    } else{
       this.append("Not equal"); 
    }

it is because result is not equal to "OK". There can be no doubt about this.

I suspect that it is one of the following:

  • There is leading or trailing whitespace on the result String; i.e. a space, TAB or CR or NL character that you can't see when you output the String to the console.

  • Possibly, the first character is an zero ('0') character instead of a capital Oh ('O').

  • Possibly, the case is different.

One way to get to the bottom of this is to take the two strings apart and compare them character by character; e.g.

String s = "OK";
if (result.equals(x)) {
    this.append("Equal Strings"); 
} else if (x.length() != result.length()) {
    this.append("Lengths are different");
} else if (x.charAt(0) != result.charAt(0)) {
    this.append("Character 0 different");
} else if (x.charAt(1) != result.charAt(1)) {
    this.append("Character 1 different");
} else {
    this.append("The universe is being swallowed by a giant squid");
}


How is this even compiling? You can't convert an int to a boolean in Java.

if(result.compareTo(x)) {

Anyway, to compare strings in Java use the equals or equalsIgnoreCase method, as follows:

System.out.println("red".equals("red")); // Prints true
System.out.println("red".equals("RED")); // Prints false
System.out.println("red".equalsIgnoreCase("RED")); // Prints true


String.compareTo() returns an int. Don't you want String.equals() which returns a boolean?

Change this.append("Not equal"); to this.append("'" + result + "' is not equal to '" + x + "'");

0

精彩评论

暂无评论...
验证码 换一张
取 消