-1

我从朋友那里得到了一个URlCaller关于连接网络服务的课程。我的假设是一切都会很好,但它包含以下错误是 J2ME 实现

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;


public class URLCaller extends Thread{
    private String url ;
    private String action;
    private URLEncoder urle;
    private String res;

    public URLCaller() {
    }

    public URLCaller(String action,String url) {
        urle = new URLEncoder();
        this.url = url;
        this.action = action;
        start();
    }

    //replace

 void authenticate(String action,String url) {
 HttpConnection connection = null;
 InputStream is = null;
 OutputStream os = null;
 StringBuffer stringBuffer = new StringBuffer();

 try {
 connection = (HttpConnection)Connector.open(url);
 connection.setRequestMethod(HttpConnection.GET);
 connection.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT");
 connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Confirguration/CLDC-1.0");
 connection.setRequestProperty("Content-Language", "en-CA");
 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 os = connection.openOutputStream();
 is = connection.openDataInputStream();
 int ch;
 while ((ch = is.read()) != -1) {
 stringBuffer.append((char) ch);
 }
 res = stringBuffer.toString() ;
 System.out.println(res);
 //textBox = new TextBox("Simple GET Test", stringBuffer.toString(), 1024, 0);

 } 
 catch(Exception e ){

 }

 finally {
     try{
           if(is!= null) {
    is.close();
 }
 if(os != null) {
    os.close();
 }
 if(connection != null) {
    connection.close();

 }
 //display.setCurrent(textBox);
     }
     catch(Exception e ){

     }

 }
}
 void sendSMS(String action,String url) {
 HttpConnection connection = null;
 InputStream is = null;
 OutputStream os = null;
 StringBuffer stringBuffer = new StringBuffer();
 //TextBox textBox = null;

 try {
 connection = (HttpConnection)Connector.open(url);
 connection.setRequestMethod(HttpConnection.GET);
 connection.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT");
 connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Confirguration/CLDC-1.0");
 connection.setRequestProperty("Content-Language", "en-CA");
 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 os = connection.openOutputStream();
 is = connection.openDataInputStream();
 int ch;
 while ((ch = is.read()) != -1) {
 stringBuffer.append((char) ch);
 }
 res = stringBuffer.toString() ;
 System.out.println(res);
 //textBox = new TextBox("Simple GET Test", stringBuffer.toString(), 1024, 0);

 } 
 catch(Exception e ){

 }

 finally {
     try{
           if(is!= null) {
    is.close();
 }
 if(os != null) {
    os.close();
 }
 if(connection != null) {
    connection.close();

 }
 //display.setCurrent(textBox);
     }
     catch(Exception e ){

     }

 }
} 

    public void run(){
        //http://message url?user=mu&password=my&from=Muyiwa&to=23475061254040&message=i+love+this.
       System.out.println(Thread.currentThread().toString() + " is running...") ;
       if (action.equals("login")){
          System.out.println(action);

           authenticate(action,url);
       }
       else if(action.equals("sendsms")) {
    System.out.println(action);

           sendSMS(action,url);
       }
    }

    public void callURL(){

        HttpConnection c = null;
        InputStream is = null;
        StringBuffer sb = new StringBuffer();
        try{
 System.out.println(url);
//url = (urle.encode(url,"UTF-8"));
//System.out.println(url);
c = (HttpConnection)Connector.open(url, Connector.READ_WRITE, true);
  c.setRequestMethod(HttpConnection.POST); //default
  is = c.openInputStream(); // transition to connected!
  int ch = 0;
  for(int ccnt=0; ccnt < 150; ccnt++) { // get the title.
    ch = is.read();
    if (ch == -1){
      break;
    }
    sb.append((char)ch);

  }
  res = sb.toString();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public String getRes() {
        return res;
    }

    public void setRes(String res) {
        this.res = res;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public URLEncoder getUrle() {
        return urle;
    }

    public void setUrle(URLEncoder urle) {
        this.urle = urle;
    }




}

Ps 有人可以将其转换为 android 实现。目前面临最后期限

4

1 回答 1

3

在 android 中有类,HttpClient并且DefaultHttpClient. 因此,您使用这些发出 Web 请求。

与您的代码发出HttpGet请求相同,并且与您的 J2ME 代码一样,您可以使用 Thread 类而不是 Thread 类AsyncTask(您也可以使用 Thread、Handler 但最好使用 Asynctask)在非 UI 线程中执行 Web 请求。

例子:

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(String result) {
            textView.setText(result);
        }
    }

现在,从您的 Android 活动代码中,您只需要execute()这个 DownloadWebPageTask即可。

像,

DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { url });

有关更多信息,请查看本教程

于 2012-06-29T09:03:11.453 回答