Preguntas y respuestas sobre cómo obtener el Agente de USUARIO para el desarrollo de Android-Android
Algunas personas en el foro publicaron algunos códigos sobre cómo obtener UA, pero los probé todos yo mismo y ninguno funcionó. Recientemente trabajé en el proyecto OPHONE y tuve muchos tratos con personas que trabajan en terminales móviles y adquirí algunos conocimientos relevantes. Aquí publico la solución autorizada. No soy el inventor del método, pero sí su practicante. Por cierto, también me gustaría agradecer a las personas relevantes aquí.
Principio:
Ubicación de almacenamiento UA de OPHONE:
1) OPHONE 1.0 y 1.5 se almacenan en /opl/etc/properties.xml
1) OPHONE 2.0 está almacenado en /opl/etc/product_properties.xml
Puedes comprobarlo tú mismo siguiendo los siguientes pasos:
1) Conéctate a un teléfono móvil o simulador .
2), ingrese adb shell
3), ingrese cd opl
4), ingrese cd, etc
5), ingrese cat properties.xml (o cat product_properties.xml OPHONE2.0)
El resultado es como se muestra a continuación:
ophone.png (6 KB)
ophone ua
p>2010-08-24 14:09
Lo anterior es el contenido de properties.xml. El siguiente paso es obtener este UA y agregarlo a su propia red. pedido.
Yo mismo escribí uno que es adecuado para las tres versiones actuales de OPHONE.
AndroidPlatform.java
paquete com.***.****;
importar java.io.ByteArrayOutputStream;
importar java.io.File;
importar java.io.FileInputStream;
clase pública AndroidPlatform {
cadena final estática pública KEYSTRING_USER_AGENT = "user_agent_key";
Cadena estática pública getUAFromProperties()
{
prueba {
FileInputStream es = getPropertyStream();
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
byte buf[] = nuevo byte[1024];
for(int k = 0; -1 != (k = is.read (buf));)
bytearrayoutputstream.write(buf, 0, k);
String fileString = new String(bytearrayoutputstream.toByteArray(), "UTF-8");
return getProperties(KEYSTRING_USER_AGENT, fileString);
//System.out.println("IS ARCHIVO Plataforma Android " + bytearrayoutputstream.size() + " "+());
} catch (Excepción e) {
// TODO: manejar excepción
System.out.println("IS FILE erororo");
e.printStackTrace();
}
return null;
}
FileInputStream público estático getPropertyStream()
{
prueba {
Propiedad del archivo = new java.io.File("/opl/etc/properties.xml");
if(property.exists())
{
return new FileInputStream(new java.io.File("/opl/etc/properties.xml"));
}
else
{
propiedad = new java.io.File("/opl/etc/product_properties.xml") ;
if(property.exists())
{
devuelve nuevo FileInputStream(new java.io.File("/opl/etc/product_properties. xml"));
}
else
{
return null;
}
}
} catch (Excepción e) {
// TODO: manejar excepción
e.printStackTrace();
}
return null;
}
Cadena estática pública getProperties(Clave de cadena, Contenido de cadena)
{
Cadena STARTKEY = "< "+key+">";
Cadena ENDKEY = ""+key+">";
contenido = content.replace("\r", "");
contenido = contenido.replace("\n", "");
int startIndex = contenido.indexOf(STARTKEY) + STARTKEY.length();
int endIndex = content.indexOf(ENDKEY);
if(startIndex > -1 && endIndex > -1)
{
return content.substring( startIndex, endIndex);
}
else
return null;
}
}
Al realizar la solicitud desde Internet, simplemente agregue UA, para que se logre la adaptación automática.
Los detalles son los siguientes:
private int CountMoneyCMWAPNEWWAY(String urlstr)
{
String strHead = "";
try{
if(!GameLet._self.isNetworkCMWAPAvailable())
{
GameLet._self.ActiveNetWorkByMode("wap");
Thread dormir(5000);
}
int splashIndex = urlstr.indexOf("/", 7);
Cadena hosturl = urlstr.substring (7, splashIndex);
String hostfile = urlstr.substring(splashIndex);
Proxy HTTPHost = new HttpHost( "10.0.0.172", 80, "http");
HttpHost target = nuevo HttpHost(hosturl, 80, "http");
HttpParams httpParams = nuevo BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000) ;
HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);
HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
HttpClientParams.setRedirecting(httpParams , verdadero) ;
String userAgent = AndroidPlatform.getUAFromProperties();
HttpProtocolParams.setUserAgent(httpParams, userAgent);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParams );
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpGet req = new HttpGet(hostfile);
HttpResponse rsp = httpclient.execute (destino, solicitud);
HttpEntity entidad = rsp.getEntity();
InputStream inputstream = entidad.getContent();
ByteArrayOutputStream bytearrayoutputstream = nuevo ByteArrayOutputStream ();
byte abyte1[] = nuevo byte[1024];
for(int k = 0; -1 != (k = inputstream.read(abyte1 ));
p>
bytearrayoutputstream.write(abyte1, 0, k);
strHead = new String(bytearrayoutputstream.toByteArray(), "UTF-8");
httpclient. getConnectionManager().shutdown();
}
catch (Excepción e) {
return 2;
}
if(strHead.indexOf("status=1301") > -1 || strHead.indexOf("status=1300") > -1)
{
devolver 1;
}
else
{
devolver 0;
}
}
Reimprimir