-1

URlCallerWeb サービスに接続するためのクラスのクラスを友人から取得しました。私の仮定では、すべてがうまくいくと思っていましたが、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にはクラスがありHttpClientDefaultHttpClient. したがって、これらを使用して Web リクエストを作成します。

コードと同じようにHttpGetリクエストを作成します。また、J2ME コードと同様に、Thread クラスの代わりに使用できますAsyncTask(スレッド、ハンドラーを使用することもできますが、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 に答える