안드로이드 개발 질문/답변
(글 수 45,052)
안드로이드에서 jsp에 연결해서 mysql에 데이터를 저장하려고 합니다.
java에서 urlconnection을 실행 시켜보고 성공하고 성공한 소스를 이용해서 안드로이드에서 시도 하고 있습니다. 하지만 getOutputStream이 실패하면서 더이상 진행되지 않고 있습니다. 검색해봐도 왜 그런가에 대한 내용은 없구요.
도와주세요
구조는 main에서 mesaage 클래스를 호출하는 형식으로 작동합니다.
주요 호출 부분에 밑줄을 그어 놨으며 작동되지 않는 부분에 빨간색 표시를 해두었습니다.
환경은 api10이고 대상핸드폰은 2.3.5 버전 htc 디자이어 입니다.
java 상태에서는 문제 없이 작동했으나.. 핸드폰에 만들어가면 문제가 생기네요..
인터넷 퍼미션 <uses-permission android:name="android.permission.INTERNET"/>은 설정한 상태입니다.
--------------MainActivity---------------
package com.example.p_ad_testhttp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties;
import android.util.Log;
import com.example.p_ad_testhttp.HttpMesaage;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String move[]=null;
try {
Log.d("test", "호출시도");
call(move);//메소드 호출
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("test", "호출실패"+e.getLocalizedMessage());
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public static void call(String[] args) throws IOException {
Log.d("test", "호출");
URL url = new URL("http://10.138.39.142:8080/gcmtest/insert.jsp);
Log.d("test", "호출2");
HttpMesaage httpMessage = new HttpMesaage(url); //객체 생성
Log.d("test", "호출3");
Properties prop = new Properties();
prop.setProperty("regid", "a2a2a2");
prop.setProperty("m", "X");
InputStream is = httpMessage.sendPostMessage(prop);//메소드 호출
BufferedReader br = new BufferedReader(new InputStreamReader(is));
char[] buff = new char[512];
int len = -1;
len = br.read(buff);
System.out.print(new String(buff, 0, len)+"1");
/* while( (len = br.read(buff)) != -1) {
System.out.print(new String(buff, 0, len));
}*/
br.close();
}
}
-==========================HttpMesaage.class=======
package com.example.p_ad_testhttp;
import java.io.*;
import java.net.*;
import java.util.*;
import android.util.Log;
/**
* HTTP 요청 메시지를 웹서버에 전송한다.
*
* @author 최범균, era13@hanmail.net
*/
public class HttpMesaage {
/**
* HTTP 프로토콜을 사용하여 연결할 URL
*/
private URL targetURL;
/**
* POST 방식으로 데이터를 전송할 때 사용되는 출력 스트림
*/
private DataOutputStream out;
public HttpMesaage(URL targetURL) {
this.targetURL = targetURL;
}
public InputStream sendGetMessage() throws IOException {
return sendGetMessage(null);
}
public InputStream sendGetMessage(Properties params) throws IOException {
String paramString = "";
if (params != null) {
paramString = "?"+encodeString(params);
}
URL url = new URL("targetURL.toExternalForm() + paramString);
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
return conn.getInputStream();
}
public InputStream sendPostMessage() throws IOException {
return sendPostMessage("");
}
//호출된 메소드
public InputStream sendPostMessage(Properties params) throws IOException {
String paramString = "";
Log.d("test", "호출인뎅");
if (params != null) {
Log.d("test", "호출인뎅2");
paramString = encodeString(params);
}
return sendPostMessage(paramString);
}
//재호출됨
private InputStream sendPostMessage(String encodedParamString) throws IOException {
URLConnection conn = targetURL.openConnection();
Log.d("test", "호출인뎅3");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
Log.d("test", "호출인뎅4");
out = null;
try {
Log.d("test", "선송시도1");
out = new DataOutputStream(conn.getOutputStream());//실행실패 부분
Log.d("test", "선송시도2");
out.writeBytes(encodedParamString);
out.flush();
} finally {
if (out != null) out.close();
}
return conn.getInputStream();
}
public static String encodeString(Properties params) {
StringBuffer sb = new StringBuffer(256);
Enumeration names = params.propertyNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String value = params.getProperty(name);
sb.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value) );
if (names.hasMoreElements()) sb.append("&");
}
return sb.toString();
}
}