我有一个包含这种类型的行的文件:
"Andorra la Vella | ad | Andorra la Vella | 20430 | 42.51 | 1.51"
我基本上只想要一个包含|之间的条目的字符串数组 分隔符:
["Andorra la Vella","ad","Andorra la Vella","20430","42.51","1.51"]
这可以用正则表达式完成吗?
我正在尝试将我从HttpPost获取的XML HttpResponse解析为服务器(last.fm),用于last.fm android应用程序.如果我简单地将其解析为字符串,我可以看到它是一个普通的xml字符串,包含所有需要的信息.但我只是无法解析单个NameValuePairs.这是我的HttpResponse对象:
HttpResponse response = client.execute(post);
HttpEntity r_entity = response.getEntity();
Run Code Online (Sandbox Code Playgroud)
我尝试了两种不同的东西,而不是它们都有效.首先,我试图检索NameValuePairs:
List<NameValuePair> answer = URLEncodedUtils.parse(r_entity);
String name = "empty";
String playcount = "empty";
for (int i = 0; i < answer.size(); i++){
if (answer.get(i).getName().equals("name")){
name = answer.get(i).getValue();
} else if (answer.get(i).getName().equals("playcount")){
playcount = answer.get(i).getValue();
}
}
Run Code Online (Sandbox Code Playgroud)
在此代码之后,name和playcount保持"空".所以我尝试使用XML Parser:
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document answer = db.parse(new DataInputStream(r_entity.getContent()));
NodeList nl = answer.getElementsByTagName("playcount");
String playcount = "empty";
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i); …Run Code Online (Sandbox Code Playgroud) 我想让用户在启动主屏幕小部件时定义用户名.但我在我的小部件中存储和访问此值时遇到了麻烦.我现在所做的是创建一个配置活动,它在创建窗口小部件时启动.因此我在清单文件中将其定义如下(我也在widgetprovider.xml中定义它):
<activity android:name=".ExampleAppWidgetConfigure">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
然后,配置活动允许用户输入将存储在SharedPreferences窗口小部件中的用户名:
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE).edit();
prefs.putString("username", text);
prefs.commit();
Run Code Online (Sandbox Code Playgroud)
我的问题是如何获取此值?在我的widget类中,我希望有一个实例变量,如:
final String userName = ...; //load the username here.
Run Code Online (Sandbox Code Playgroud)
但是如何在我的小部件中检索用户名?问题是widget是AppWidgetProvider我无法访问的子类SharedPreferences(因为AppWidgetProvider它不是子类Context).我想将用户名存储在strings.xml文件中,这似乎也不可能.使用SharedPreferences正确的方法吗?我还想过将用户名设置TextView为我的小部件,后来应该显示用户名.但我需要在我的类中用于一些服务器请求的用户名,似乎没有办法获取TextView小部件中的文本...只setTextViewText(String text)定义了setter .
我该如何检索用户名?
我是网络编程的初学者.我试图创建一个从SQL数据库中读取数据的简单网站.起初我只是写了我的数据库密码并直接登录到php代码:
<?php
$username = "login";
$password = "pw";
mysql_connect("server", $username, $password);
...
?>
Run Code Online (Sandbox Code Playgroud)
这显然不是一个好主意!那么(更多)更"安全"的方式是什么呢?我读到将php代码放入一个单独的文件中,这意味着不要进入网站的主要php文档,然后限制对该文件的访问.也许是通过.htaccess文件.这是要走的路吗?
所以我需要两个文件作为我的mapreduce程序的输入:City.dat和Country.dat
在我的main方法中,我解析命令行参数,如下所示:
Path cityInputPath = new Path(args[0]);
Path countryInputPath = new Path(args[1]);
Path outputPath = new Path(args[2]);
MultipleInputs.addInputPath(job, countryInputPath, TextInputFormat.class, JoinCountryMapper.class);
MultipleInputs.addInputPath(job, cityInputPath, TextInputFormat.class, JoinCityMapper.class);
FileOutputFormat.setOutputPath(job, outputPath);
Run Code Online (Sandbox Code Playgroud)
如果我现在使用以下命令运行我的程序:
hadoop jar capital.jar org.myorg.Capital /user/cloudera/capital/input/City.dat /user/cloudera/capital/input/Country.dat /user/cloudera/capital/output
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Exception in thread "main" org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory /user/cloudera/capital/input/Country.dat already exists
Run Code Online (Sandbox Code Playgroud)
为什么将它视为我的输出目录?我指定了另一个目录作为输出目录.有人可以解释一下吗?
我需要创建围绕其中心旋转的矩形(因此它们不需要与坐标系的轴平行).所以basicly每个矩形可以由center-X,center-Y,width,height和angle定义.我想要做的是对这些矩形中是否包含某些点进行计算(因此不涉及绘图).我想我不能使用Rectangle2D该类,因为这些矩形将始终与坐标系的x和y轴平行.是通过编写我自己的矩形类来获得此功能的唯一方法,还是Rectangle2D我可以使用的任何现有(类似)?
我有 Rails 和 Jquery 的问题。我使用 AJAX 向文章添加评论,而无需重新加载它们。以下代码自动包含在我的views/application.html中:
<%= javascript_include_tag "application" %>
<%= javascript_include_tag :all %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" %>
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" %>
Run Code Online (Sandbox Code Playgroud)
一切似乎都工作正常,直到我意识到服务器控制台显示以下错误:
ActionController::RoutingError (No route matches [GET] "assets/all.js")
Run Code Online (Sandbox Code Playgroud)
因此,由于除了错误之外,这一行似乎没有向应用程序添加任何内容,因此我将其删除。下次我启动服务器并使用该应用程序时,突然每个评论都会发布两次!?否则一切仍然正常。所以我再次添加了删除的行,我不知道为什么,但是当我添加该行时
<%= javascript_include_tag :all %>
Run Code Online (Sandbox Code Playgroud)
再次一切正常,只有一条评论按预期发布。但是我不想将其保留在代码中,因为它会引发错误。有人可以解释这种行为并告诉我如何解决这个问题吗?