iアプリでのHTTP通信について
- iアプリでのHTTP通信について困っています。現在、iアプリで業務アプリの開発を行っていますが、HTTP通信によりサーバ上のテキストファイルを読み込むことができません。
- 開発環境はdoja5.1+eclipse3を使用し、ADFの設定ではPackageURL及びUseNetworkを設定しています。しかし、デバッグ起動すると「httpc.connect();」でConnectionExceptionが発生し、statusの値は0です。
- APIリファレンスを参照しても「UNDEFINED:未定義の例外を表します」とあり、どのような情報でも助かります。
- ベストアンサー
iアプリでのHTTP通信について
こんばんわ。大変困っています。 現在、iアプリで業務アプリの開発を行っています。 HTTP通信によりサーバ上のテキストファイルを読み込もうとしているのですがどうもうまくいきません。 開発環境はdoja5.1+eclipse3を使用しております。 ADFの設定はPackageURL及びUseNetworkを設定しております。 この状態で以下のソースコードをデバッグ起動すると「httpc.connect();」にてConnectionExceptionが発生し、statusの値は0です。 APIリファレンスを参照しても「UNDEFINED:未定義の例外を表します」とあり、何のこっちゃわかりません。 どんな情報でもいいので宜しくお願いいたします。 以下はHTTP通信部分のソースです。 public String HttpGetTest(){ String strRet=""; try { HttpConnection httpc = (HttpConnection)Connector.open(getSourceURL()+"test.txt",Connector.READ,true); httpc.setRequestMethod(HttpConnection.GET); httpc.connect(); int respCode=httpc.getResponseCode(); String respMsg=httpc.getResponseMessage(); long contentLength=httpc.getLength(); if (respCode == HttpConnection.HTTP_OK) { byte rcvData[] = new byte[(int)contentLength]; InputStream ips=httpc.openInputStream(); ips.read(rcvData); ips.close(); strRet = new String(rcvData); } httpc.close(); }catch(ConnectionException ce){ Dialog dlgWarning = new Dialog(Dialog.DIALOG_ERROR,"ConnectionException"); dlgWarning.setText(ce.toString() + ":" + ce.getStatus()); dlgWarning.show(); } catch(Exception e){ Dialog dlgWarning = new Dialog(Dialog.DIALOG_ERROR,"Exception"); dlgWarning.setText(e.toString()); dlgWarning.show(); } return strRet; }
- mw43
- お礼率75% (3/4)
- Java
- 回答数3
- ありがとう数2
- みんなの回答 (3)
- 専門家の回答
質問者が選んだベストアンサー
- ベストアンサー
では、PCの環境周りでは大丈夫でしょうか? ブラウザから対象のURLにはアクセスできますか? HTTPプロキシを使っている場合は、エミュレータにその設定がされていますか?
その他の回答 (2)
getSourceURL()の戻り値を、エミュレータと実機で確認してみてください。 エミュレータの[設定]->[アプリケーション動作環境設定]->[ネットワーク設定]->ADFのURL の設定は正しいですか?
補足
回答ありがとうございます。 さっそく確認いたしました。 PackageURLの設定値は"http://xxxx.xxxxxxxxxx.co.jp/iapr/take/UnUn.jar"です。 実機及びエミュレータにてgetSourceURL()の値を確認しましたが 両方とも"http://xxxx.xxxxxxxxxx.co.jp/iapr/take/"でした。 (xxxx.xxxxxxxxxxの部分は実際とは異なります) よってPackageURLの設定は正しいと思われます。 宜しくお願い致します。
- _ranco_
- ベストアンサー率58% (126/214)
Connector.openで接続が成立しているので、connectコールは要らないはず。
お礼
回答ありがとうございます。 connectコールをコメントアウトすると「Illegal connection object state」の例外が発生します。 どうやらconnectコールで接続確立のようです。
補足
実は進展?がありました。 上記ソースを含んだiアプリを実機で起動するとテキストファイルが読め、思惑通りの動きをしました。 ただ、エミュレータでは例外は発生します。 どうやら原因はeclipse又はdojaの環境設定と思われます。 このまま実機でのデバッグでは非常に効率が悪いので何とか解決したいと思いますのでアドバイスをお願いいたします。 ちなみにADFの設定はAppName,PackageURL,UseNetwork以外に設定が必要なものはあるでしょうか? 宜しくお願いいたします。
関連するQ&A
- DoJa5.1でiアプリを作っています。
DoJa5.1でiアプリを作っています。 サーバー上のテキストファイルの内容を、スクラッチパッドに保存しようとしています。 サーバ(さくらのレンタルサーバ)がcontentLengthを返していないようで、 うまく動作しません。 サーバ上のテキストファイルはサイズが一定ではありません。 contentLengthを取得する方法、または別の解決方法があったら教えてください。 よろしくお願いします。 HttpConnection http = null; InputStream in = null; data = null; try{ http = (HttpConnection)Connector.open("http://xxxxx.sakura.ne.jp/cgi-bin/xxxxx.cgi", Connector. READ); http.setRequestMethod(HttpConnection.GET); http.setRequestProperty("Content-Type","text/plain"); http.connect(); int contentLength = (int)http.getLength(); in = http.openInputStream(); // textbox.setText(Integer.toString(contentLength)); // contentLengthの値を確認したらなにもない(空欄のまま) // ちなみに、5行まえの「http.setRequestProperty("Content-Type","text/plain");」 // をコメントアウトすると、contentLengthは「-1」になりました。 // data = new byte[contentLength]; // contentLengthが取得できないので、ここでエラーになる // in.read(data); } catch... サーバ上のCGIです。 -------------------------------------------------------------- #!/usr/bin/perl print "Content-type: text/plain\n\n"; print "test"; --------------------------------------------------------------
- ベストアンサー
- Java
- iアプリで、配列の使い方がわからず困っています。
iアプリで、あるクラスの配列を別のクラスで利用したいのですが、その方法がよくわかりません。 たとえば、Itemクラスの配列の任意の要素を、Testクラスで使用したい場合、どのようにすればよいのでしょうか。 ========== Item.java ========== public class Item { String Item[] = new String[700]; Item(){ Item[0] = new String("item0"); Item[1] = new String("item1"); Item[2] = new String("item2"); ・ ・ ・ Item[699] = new String("item699"); } } ========== Test.java ========== public class Test extends IApplication { public void start(){ /* Itemクラスの配列'Item'を使用するための記述がわかりません。 */ Dialog _dialog=new Dialog(Dialog.DIALOG_INFO,"Item"); _dialog.setText("Item:" + Item[258]); // ← 配列'Item'の任意の要素(Item[258])を適用したい。 _dialog.show(); terminate(); } }
- ベストアンサー
- Java
- iアプリとサーブレットの連動(eclipse)
はじめまして、java初心者の者です。 現在私は、eclipseを使って、iアプリ開発に挑戦しています。 その中で、iアプリとサーブレットを連動させる方法が分からず、困っています。 iアプリ上の画面で、aと入力してサーブレットに送ると、サーブレットでテキストデータを書き換え、Aに変更して送り返すという、本当に初歩的な動作を試したいのですが、上手く連動してくれません。 ネット上のサンプルソースを丸写しで使っても動かないため、おそらく設定やファイルの置き場所、接続先の記述などに問題があるのだと思います。 インターネット等でいろいろと情報を集めてみたのですが、そもそもeclipseの中でDoJaプロジェクトを作成した場合、サーブレットを使うにはどのような設定が必要なのか、またデータはどのフォルダへ保存するべきなのかといった基礎的な情報が収集できず、困っています。 tomcatプロジェクトを作成し、その中でjspとサーブレットを連動させるテストは成功したのですが、DoJaプロジェクトではweb.xmlファイルを作らなくていいのか?など、本当に基礎的な部分が全く理解できていません・・・。 また、以下がネット上で拾ったサンプルコードです。 同じフォルダにあるサーブレット(Test)と連動させたいと思っているのですが、下記に何か間違いはないのでしょうか? ---------------------------------------------------------------- import com.nttdocomo.ui.*; import java.io.*; import com.nttdocomo.io.*; import javax.microedition.io.*; public class A extends IApplication { public void start() { String res = http("servlet/Test?test=a"); showDialog(res); } void showDialog(String s) { Dialog d = new Dialog(0, "dialog"); d.setText("" + s); d.show(); } String http(String name) { String url = IApplication.getCurrentApp().getSourceURL(); url += name; String res = null; HttpConnection conn = null; try { conn = (HttpConnection)Connector.open(url, Connector.READ); conn.setRequestMethod(HttpConnection.GET); conn.connect(); InputStream in = conn.openInputStream(); int len = (int)conn.getLength(); byte[] buf = new byte[len]; int off = 0; for (;;) { int n = in.read(buf, off, len); if (n < 0) throw new Exception(); off += n; len -= n; if (len == 0) break; } in.close(); conn.close(); return new String(buf); } catch (Exception e) { try { conn.close(); } catch (Exception e2) { } } return null; } } --------------------------------------------------------------- 本当に基礎的な質問で申し訳ありません。 どなたかご存知の方がいらっしゃいましたら、何卒よろしくお願いいたします。 ※以下、私の環境をお伝えいたします。 ・eclipse3.1 ・DoJa-5.1 ・apache-tomcat-6.0.20
- ベストアンサー
- Java
- dojaのHTTP通信時Exception
iアプリでアプリの開発を行っています。 開発環境はdoja5.0+eclipse3.1を使用しております。 HTTP通信によりサーバにバイナリデータを送信したいのです ADFの設定はPackageURL及びUseNetworkを設定しております。 localhostでエミュレータでテストしているため PackageURLは「http://localhost:80/dictionary.jar」と設定しています。 以下のソースコードをデバッグ起動すると「httpCon.connect();」にて exceptionが発生して、「Illegal connection object state」のいう メッセージがかえってきます。 以下はHTTP通信部分のソースです。 どうか知恵をお貸しください。 よろしくお願いいたします。 // URLを作成する String url = IApplication.getCurrentApp().getSourceURL() + "getScore.php"; StringBuffer reqMsg = new StringBuffer(); DataInputStream fromStream = null; DataOutputStream toStream = null; HttpConnection httpCon = null; try { // HTTP接続処理 httpCon = (HttpConnection)Connector.open(url, Connector.READ_WRITE, true); httpCon.setRequestMethod(HttpConnection.POST); httpCon.setRequestProperty("Content-Type", "application/x-xpinstall"); //送信データ元(スクラッチパッド頭8000バイト分) fromStream = Connector.openDataInputStream("scratchpad:///0;pos=0,length=8000"); //送信データ先 toStream = httpCon.openDataOutputStream(); byte[] buff = new byte[8000]; fromStream.read(buff); toStream.write(buff); //http接続 httpCon.connect();←ここでException
- ベストアンサー
- Java
- iアプリの作成で画像が再描画されません。
iアプリの作成で画像が再描画されません。 どうしても解決できないのでご教示下さい。 iアプリの作成をしながら勉強しています。 利用しているプロファイルはdoja 5.1です。 class MainCanvas extends Canvas { public void paint(Graphics g) { MediaImage mi = MediaManager.getImage("resource:///6.gif"); try { mi.use(); } catch(Exception e) { //何らかの例外処理 } Image image = mi.getImage(); getGraphics().drawImage(image, 20, 0); } public void processEvent(int type, int param) { if (type==Display.KEY_PRESSED_EVENT) { if (param == Display.KEY_SELECT) { Dialog dlg = new Dialog(Dialog.DIALOG_YESNO, "終了"); dlg.setText("終了しますか?"); int result = dlg.show(); if (result == Dialog.BUTTON_YES) { IApplication.getCurrentApp().terminate(); } } if (param == Display.KEY_SOFT1) { MediaImage mi = MediaManager.getImage("resource:///9.gif"); try { mi.use(); } catch(Exception e) { //何らかの例外処理 } Image image = mi.getImage(); getGraphics().drawImage(image, 0, 0); } } } } こんなクラスがあった時、ダイアログがキャンセルされると、 「9.gif」の方がキャンバス上から消えてしまいます。 どういう作りにすると、キャンバスから画像が消えなくなる のでしょうか?
- ベストアンサー
- Java
- iアプリ Tomcatと連携
前回の質問は、分かりづらいようなので新たに質問させていただきます。 現在、TomcatとiアプリDoja_Ver5.1、MySQLを使いDojaの方からTomcatへアクセスしTomcatからMySQLのデータを取りDojaの方へ表示させたいのですが、下記の文で何度やっても「java.lang.SecurityException: Illegal host」と出てしまいできません。 ADFファイルである、Jamファイルは、Jarファイルと同じ場所にあるので、PackageURLには「dosei_jisaku.jar」としか記述していません。 何故か、java.lang.SecurityException: Illegal hostが出て解決策も見出せずでいます;; public class dosei_jisaku extends IApplication { public void start() { mypanel wp_mypanel = new mypanel(); Display.setCurrent(wp_mypanel); } class mypanel extends Panel{ public mypanel() { add(new Label("項目名:")); TextBox w_name = new TextBox("",8,1,TextBox.DISPLAY_ANY); add(w_name); try { HttpConnection c = (HttpConnection)Connector.open("http://localhost:8080/mfs/mfs/test/dosei_jisakuSV"); ←同PCにあるTomcatのServlet(classファイル)へアクセスしています。 c.setRequestMethod(HttpConnection.GET); c.connect(); if(c.getResponseCode() != HttpConnection.HTTP_OK){ throw new IOException(); } InputStream is = c.openInputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream(); for(;;){ int n = is.read(); if (n == -1) { break; } os.write(n); } String str = new String(); str = os.toString(); w_name.setText(str); os.close(); is.close(); c.close(); } catch(Throwable e) { w_name.setText("読込エラー" + e); } } } } どなたかアドバイスを頂けるようお願い致します…。
- ベストアンサー
- その他([技術者向] コンピューター)
- コネクションを使いまし??? au携帯でオープンアプリで通信のテスト
au携帯でオープンアプリで通信のテストをしています。 HTTP GETメソッドを実行する には以下のようにすれば良いのですが、 下のreadHttpConnection関数を呼び出すごとに、 通信確認 このオープンアプリは通信を行おうとしています。 通信を許可しますか? ●許可する ○許可しない となります、以前の質問でコネクションを使いましをすれば、最初の1回で済むと回答を得ましたが、どのように使いまわしをすればそうなるのかわかりません。 conn = (HttpConnection)Connector.open(url, Connector.READ, true); ここに入力するURLは、毎回異なるため(例えば http://test?s=1 や http://test?s=2 など) どうしたらよいのでしょうか? //=== ソース === int readHttpConnection(String url, byte[] data) throws Exception { HttpConnection conn = null; InputStream in = null; int i, length = 0; try { conn = (HttpConnection)Connector.open(url, Connector.READ, true); conn.setRequestMethod(HttpConnection.GET); in = conn.openInputStream() ; while((i = in.read(data, length, data.length - length)) > 0) length += i; } catch(Exception ex) { } finally { if(in != null) in.close(); if(conn != null) conn.close(); } return length; }
- ベストアンサー
- Java
- java通信について
アプレットビューアは 通信成功できますが HTMLから実行すると失敗します so = new Socket( "localhost", 80 ); この部分が失敗してるようです なぜ起こるんでしょうか ? 解決方法を教えてください。 //html <html><body> <applet code = "a.class" width = "370" height ="530"></applet> </body></html> //<applet code = "a.class" width="400" height = "200"></applet> import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class a extends java.applet.Applet implements ActionListener{ TextField tf; Button b; TextArea t; Panel p, p1; String str = "http://localhost/cgi-bin/a.cgi"; public void init(){ tf = new TextField( 32 ); b = new Button( "Post" ); t = new TextArea( 20, 48 ); b.addActionListener( this ); p = new Panel(); p.add( new Label( "String" ) ); p.add( tf ); p.add( b ); p1 = new Panel(); p1.add( t ); add( p ); add( p1 ); } public void actionPerformed( ActionEvent e ){ String str; if( e.getSource() == b ){ t.setText( sen( tf.getText() ) ); } } private String sen( String a ){ Socket so = null; InputStreamReader in = null; OutputStream os = null; String str1 = new String(); try { t.setText( "aa" ); so = new Socket( "localhost", 80 ); t.setText( "bb" ); in = new InputStreamReader( so.getInputStream() ); os = so.getOutputStream(); }catch( Exception e ){ return "Error!"; } String H = "POST " + str + " HTTP/1.0\n"; H += "Content-Length:" + String.valueOf( a.length() ) + "\n\n"; String st = new String( H + a ); try{ os.write( st.getBytes() ); }catch( Exception e ){ return "Error!"; } str1 = "Send:\n" + st; boolean bo = true; StringBuffer sb = new StringBuffer(); int s; while( bo ){ try{ s = in.read(); if( s == -1 ) bo = false; else sb.append( (char)s ); }catch( Exception e ){ bo = false; } } str1 += "\nResv:\n" + sb.toString(); return str1; } }
- ベストアンサー
- Java
- UDP通信(SNMP)したいが、うまく動かない。
下記URLの質問の続きのような質問なのですが…。 SNMPで応答を得ようと、下記のようにコードを書いてみたのですが、うまくいきません。 足りないところ、間違っているところ、こうしたらいいんじゃない?等、簡単なことでも結構です。 ご教授いただけたらと思います。 よろしくお願いします。 http://oshiete1.goo.ne.jp/qa4934857.html //ここからコード public class SNMP_UDPReceive2 { public static void main(String[] arg){ System.out.println("hajimata!"); try{ byte buf[] = "Get".getBytes(); //ソケットを作成 DatagramSocket soc = new DatagramSocket(); //パケットを作成(""の中身は例です。) DatagramPacket packet = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.0.1"),161); //送信 soc.send(packet); //受信処理 byte buf_re[] = new byte[512]; DatagramPacket packet_re = new DatagramPacket(buf,buf.length); soc.receive(packet_re); //受信したデータを取得 String data = new String(packet_re.getData(),0,packet_re.getLength()); System.out.println("\"" + data + "\"を受信しました"); }catch(Exception e){ e.printStackTrace(); } } }
- ベストアンサー
- Java
- HTTPでPOSTしてgooメールにログイン
Javaを使用して、HTTPでgooメールにログインしたいのですが、うまくいきません。 サンプルを探して試しているのですが、レスポンスに「302」が返却されます。 HTTPヘッダの情報が足りないのか、POSTするデータが足りないのか、 はたまた、その他なにかしらが足りないのか、ご存知の方がおりましたら、教えて頂けないでしょうか? 以下apacheのhttpclientを使ったレスポンスに「302」が返却されるプログラムです。 public class http_test { public static void main(String[] args) { try { String url = "https://login.mail.goo.ne.jp/id/authn/Login"; String id = "gooID"; // gooID ※質問用にダミー値を設定 String pass = "password"; // パスワード ※質問用にダミー値を設定 String params = "uname="+ id +"&pass=" + pass + "&Site=mail.goo.ne.jp&Success=http://mail.goo.ne.jp"; HttpPost httpPost = new HttpPost( url ); DefaultHttpClient httpClient = new DefaultHttpClient(); StringEntity paramEntity = new StringEntity( params ); paramEntity.setChunked( false ); paramEntity.setContentType( "application/x-www-form-urlencoded" ); httpPost.setEntity( paramEntity ); HttpResponse response = httpClient.execute( httpPost ); int status = response.getStatusLine().getStatusCode(); if ( status != HttpStatus.SC_OK ) { // ここで「302」が返却される System.out.println(status); throw new Exception( "" ); } }catch(Exception e) { e.printStackTrace(); } } }
- 締切済み
- Java
お礼
アドバイスありがとうございます。 HTTPプロキシのキーワードから無事解決いたしました。 原因はdojaのネットワーク設定にあるHTTPプロキシ関連項目に不要な値を設定していたためでした。 (HTTPプロシキを使用していないのに値を設定していた) 環境構築時に意味もなく設定していたようです。。。 お騒がせ致しました。 以上