AndroidのプログラムでFlurryAnalyticsを使用しようとしていますが、サーバーからxmlファイル自体を取得できません。
Log Cat System.outタグで何らかの理由で半分を取得でき、「XML Passing Exception = Java.net.MalformedURLException:Protocol not found:?xml version = 1.0 encoding = "UTF -8 "など... xmlコードの約半分まで。何が間違っているのかわからないので、application/xmlの受け入れを要求するヘッダー付きのHTTPgetを送信していますが、正しく機能していません。ヘルプありがたいです!
try {
//HttpResponse response = client.execute(post);
//HttpEntity r_entity = response.getEntity();
//String xmlString = EntityUtils.toString(r_entity);
HttpClient client = new DefaultHttpClient();
String URL = "http://api.flurry.com/eventMetrics/Event?apiAccessCode=????&apiKey=??????&startDate=2011-2-28&endDate=2011-3-1&eventName=Tip%20Calculated";
HttpGet get = new HttpGet(URL);
get.addHeader("Accept", "application/xml");
get.addHeader("Content-Type", "application/xml");
HttpResponse responsePost = client.execute(get);
HttpEntity resEntity = responsePost.getEntity();
if (resEntity != null)
{
System.out.println("Not null!");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String responseXml = EntityUtils.toString(responsePost.getEntity());
Document doc = db.parse(responseXml);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("eventMetrics");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("day");
Element dayElement = (Element) nameList.item(0);
nameList = dayElement.getChildNodes();
countString = dayElement.getAttribute("totalCount");
System.out.println(countString);
count = Integer.parseInt(countString);
System.out.println(count);
count += count;
}
}
} catch (Exception e) {
System.out.println("XML Passing Exception = " + e);
}
文字列を受け取るparseメソッドは、URL形式用です。文字列を解析する前に、StringReaderで文字列をラップする必要があります。 XMLをInputStreamとして取得し、次のように解析できるとさらに便利です。
String uri =
"http://api.flurry.com/eventMetrics/Event?apiAccessCode=?????&apiKey=??????&startDate=2011-2-28&endDate=2011-3-1&eventName=Tip%20Calculated";
URL url = new URL(uri);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
InputStream xml = connection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xml);
HttpURLConnectionを使用しましたが、これは作業コードです。
URL url = new URL("....");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Accept", "application/xml");
httpConnection.setRequestProperty("Content-Type", "application/xml");
httpConnection.setDoOutput(true);
OutputStream outStream = httpConnection.getOutputStream();
OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8");
outStreamWriter.write(requestedXml);
outStreamWriter.flush();
outStreamWriter.close();
outStream.close();
System.out.println(httpConnection.getResponseCode());
System.out.println(httpConnection.getResponseMessage());
InputStream xml = httpConnection.getInputStream();