私はこれを2週間の大部分で試してきましたが、本当に行き詰まっています。最初に、クライアントがAndroidアプリである単純なObjectOutputStreamクライアント(サーバープログラム)を作成しましたが、機能しません(接続は読み取りますが、オブジェクトは読み取りません)。
だから今、私はこの単純なタスクを実行するために私が取ることができるかもしれない他のアプローチについて混乱していますか?誰か助けてもらえますか?
これを試してみませんか:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
postメソッドを使用してURLConnectionを試しましたか?:)
または、次のようなメソッドを取得します。
String yourURL = "www.yourwebserver.com?value1=one&value2=two";
URL url = new URL(yourURL);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
response = in.readLine();
データを送信するために JSON 攪拌を試すことができます。JSON を操作する方法については多くの資料があり、多くの API もあります。JSONSimple は私が提案できるものです。とても簡単です。
これを使用してエンティティをサーバー に投稿できます。
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
postRequest.setEntity(entity);
try {
HttpResponse response = httpClient.execute(postRequest
);
String jsonString = EntityUtils.toString(response
.getEntity());
Log.v(ProgramConstants.TAG, "after uploading file "
+ jsonString);
return jsonString;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
エンティティは、名前と値のペアにすることができます:
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("key1", value1));
nvps.add(new BasicNameValuePair("key2", value2));
Entity entity=new UrlEncodedFormEntity(nvps, HTTP.UTF_8)
または、bytearray を使用してエンティティを送信できます。
Bitmap bitmapOrg=getBitmapResource();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] data = bao.toByteArray();
MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
entity.addPart("file", new ByteArrayBody(data, "image/jpeg",
"file"));
json をサーバーに投稿する場合:
このリンクを確認してくださいAndroid アプリケーションからサーバーへの POST 要求で JSON を BODY として送信するにはどうすればよいですか?
Java オブジェクトのシリアル化と逆シリアル化については、https:
//sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson をお勧めします。サーバーへのデータ送信の概要を理解するのに役立つことを願っています