안드로이드 환경으로 카카오페이 단건 결제를 구현하고 있습니다.
HOST 주소를 이용할 때 FileNotFoundException이 발생합니다.
BufferedReader를 새로 선언하는 부분에서 AsyncTask로 잠시 넘어가는데, return connection.getInputStream (); 부분입니다.
예시 코드도 없어서 어떻게 고쳐야 할지 모르겠습니다.
W/System.err: java.io.FileNotFoundException: http://kapi.kakao.com/v1/payment/ready
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:255)
java.lang.NullPointerException
`URL url = new URL ( "http://kapi.kakao.com/v1/payment/ready" );
final HttpURLConnection connection = ( HttpURLConnection ) url.openConnection ();
connection.setRequestMethod ( "POST" );
connection.setRequestProperty ( "Authorization" , "KakaoAK " + "129f7b1a33d3f326d004d1242610398a" );
connection.setRequestProperty ( "Content-Type" , "application/x-www-form-urlencoded;charset=utf-8" );
connection.setDoInput ( true );
connection.setDoOutput ( true );
Map < String, String > params = new HashMap < String, String > ();
params.put ( "cid" , "TC0ONETIME" );
params.put ( "partner_order_id" , "partorder" );
params.put ( "partner_user_id" , "0001" );
params.put ( "item_name" , "Deliver" );
params.put ( "quantity" , "1" );
params.put ( "total_amount" , "3000" );
params.put ( "tax_free_amount" , "3000" );
params.put ( "approval_url" , "http://localhost:8080/kakaoPaySuccess" );
params.put ( "cancel_url" , "http://localhost:8080/kakaoPayCancel" );
params.put ( "fail_url" , "http://localhost:8080/kakaoPaySuccessFail" );
String string_params = new String ();
for ( Map.Entry < String, String > elem : params.entrySet () )
{
string_params += ( elem.getKey () + "=" + elem.getValue () + "&" );
}
AsyncTask < Void, Void, OutputStream > asyncTaskOut = new AsyncTask < Void, Void, OutputStream > ()
{
@Override
protected OutputStream doInBackground ( Void... voids )
{
try
{
return connection.getOutputStream ();
} catch ( IOException e )
{
e.printStackTrace ();
}
return null;
}
};
AsyncTask < Void, Void, InputStream > asyncTaskIn = new AsyncTask < Void, Void, InputStream > ()
{
@Override
protected InputStream doInBackground ( Void... voids )
{
try
{
return connection.getInputStream ();
} catch ( IOException e )
{
e.printStackTrace ();
}
return null;
}
};
try
{
OutputStream outputStream = asyncTaskOut.execute ().get ();
outputStream.write ( string_params.getBytes () );
outputStream.flush ();
outputStream.close ();
} catch ( ExecutionException e )
{
e.printStackTrace ();
} catch ( InterruptedException e )
{
e.printStackTrace ();
}
try
{
BufferedReader bufferedReader = new BufferedReader ( new InputStreamReader ( asyncTaskIn.execute ().get () ) );
String jsonText = readAll ( bufferedReader );
JSONObject obj = new JSONObject ( jsonText );
url = ( URL ) obj.get ( "next_redirect_pc_url" );
} catch ( ExecutionException e )
{
e.printStackTrace ();
} catch ( InterruptedException e )
{
e.printStackTrace ();
} catch ( JSONException e )
{
e.printStackTrace ();
}`